// Created by Ydw on 16-03-17.// Copyright (c) 2016年 com. All rights reserved.//#import <UIKit/UIKit.h>@classViewControllerA;@protocolViewControllerAdelegate-(void)changeBgColorFromCtrlA:(ViewControllerA *)aView withColor:(UIColor *)color;@end@interfaceViewControllerA : UIViewController@property( assign, nonatomic ) id< ViewControllerAdelegate > delegate;-(IBAction)changeColorAction:(id)sender;
-(IBAction)changeColorAction2:(id)sender;@end
ViewControllerA的.m文件
// Created by Ydw on 16-03-17.// Copyright (c) 2016年 com. All rights reserved.//#import "ViewControllerA.h"#import "ViewControllerB.h"@interfaceViewControllerA ()@end@implementationViewControllerA@synthesize delegate;- (void)viewDidLoad
{[super viewDidLoad];self.title = @"Ctrl A";
}//使用協議代理Delegate
- (IBAction)changeColorAction:(id)sender {[delegate changeBgColorFromCtrlA:self withColor:[UIColor grayColor]];[[self navigationController] popViewControllerAnimated:YES];
}//通知中心NSNotificationCenter,發送通知
- (IBAction)changeColorAction2:(id)sender {UIColor *color = [UIColor greenColor];[[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeColorKey" object:color];[[self navigationController] popViewControllerAnimated:YES];
}- (void)didReceiveMemoryWarning
{[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}@end
ViewControllerB的.h文件
// Created by Ydw on 16-03-17.// Copyright (c) 2016年 com. All rights reserved.//#import <UIKit/UIKit.h>#import "ViewControllerA.h"@interfaceViewControllerB : UIViewController<ViewControllerAdelegate>@end
ViewControllerB的.m文件
// Created by Ydw on 16-03-17.// Copyright (c) 2016年 com. All rights reserved.//#import "ViewControllerB.h"@implementationViewControllerB- (void)viewDidLoad
{[super viewDidLoad];self.title = @"Ctrl B";self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Go CtrlA" style:UIBarButtonItemStylePlain target:self action:@selector(go)];//注冊通知[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor:) name:@"ChangeColorKey" object:nil];
}-(void)go {UIStoryboard *stryBoard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];ViewControllerA *_ViewControllerA = [stryBoard instantiateViewControllerWithIdentifier:@"ViewControllerA"];_ViewControllerA.delegate = self;[[self navigationController] pushViewController:_ViewControllerA animated:YES];_ViewControllerA = nil;}//響應協議代理Delegate
-(void)changeBgColorFromCtrlA:(ViewControllerA *)aView withColor:(UIColor *)color{[self.view setBackgroundColor:color];
}//響應通知中心NSNotificationCenter
- (void)changeColor:(NSNotification *)notification{if([notification object] != nil){UIColor *color = [notification object];[self.view setBackgroundColor:color];}
}- (void)dealloc{//移除通知[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ChangeColorKey" object:nil];
}- (void)didReceiveMemoryWarning
{[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}@end
KVC KVC是Key Value Coding的縮寫,即是鍵值編碼。在iOS中,提供了一種方法通過使用屬性的名稱(也就是Key)來間接訪問對象的屬性方法。實際上,就是通過類定義我們可以看到類的各種屬性,那么使用屬性的名稱就能訪問到類實例化后的對象的這個屬性值。這個方法可以不通過getter/setter方法來訪問對象的屬性。 NSArray/NSSet等都支持KVC。
定義一個myInformation的類
@interfacemyInformation : NSObject {NSString *_name; int _age; int _height; int _weight;
}
@end