方法三:代碼塊
根視圖代碼:
//.m文件
-?(void)viewDidLoad
{
[super?viewDidLoad];
self.view.backgroundColor?=?[UIColor?redColor];
[self?createButton];
}
-?(void)createButton
{
UIButton?*?btn?=?[UIButton?buttonWithType:UIButtonTypeCustom];
btn.frame?=?CGRectMake(10,?30,?300,?40);
[btn?setTitle:@"進入下一個視圖控制器"forState:UIControlStateNormal];
btn.layer.cornerRadius?=?5;
btn.backgroundColor?=?[UIColor?blackColor];
[btn?addTarget:self?action:@selector(btnClick)?forControlEvents:UIControlEventTouchUpInside];
[self.view?addSubview:btn];
}
-?(void)btnClick
{
Sub1ViewController?*?sub1?=?[[Sub1ViewController?alloc]init];
sub1.view.backgroundColor?=?[UIColor?blueColor];
sub1.MyBlock?=?^(UIColor?*?color)
{
self.view.backgroundColor?=?color;
};
[self?presentViewController:sub1?animated:YES?completion:nil];
}
子視圖代碼
//.h文件
@interface?Sub1ViewController?:?UIViewController
@property?(copy,nonatomic,readwrite)void(^MyBlock)(UIColor?*?color);
@end
//.m文件
-?(void)viewDidLoad
{
[super?viewDidLoad];
[self?createPopToRootViewBtn];
}
-?(void)createPopToRootViewBtn
{
UIButton?*?btn?=?[UIButton?buttonWithType:UIButtonTypeCustom];
btn.frame?=?CGRectMake(10,?30,?300,?40);
[btn?setTitle:@"進入根視圖控制器"forState:UIControlStateNormal];
btn.layer.cornerRadius?=?5;
btn.backgroundColor?=?[UIColor?blackColor];
[btn?addTarget:self?action:@selector(btnClick)?forControlEvents:UIControlEventTouchUpInside];
[self.view?addSubview:btn];
}
-?(void)btnClick
{
_MyBlock([UIColor?orangeColor]);
[self?dismissViewControllerAnimated:YES?completion:nil];
}
方法四:代理-協(xié)議
根視圖代碼
//.h
#import?
#import?"Sub1ViewController.h"
@interface?ViewController?:?UIViewController
@end
//.m
-?(void)viewDidLoad
{
[super?viewDidLoad];
self.view.backgroundColor?=?[UIColor?redColor];
[self?createButton];
}
-?(void)createButton
{
UIButton?*?btn?=?[UIButton?buttonWithType:UIButtonTypeCustom];
btn.frame?=?CGRectMake(10,?30,?300,?40);
[btn?setTitle:@"進入下一個視圖控制器"forState:UIControlStateNormal];
btn.layer.cornerRadius?=?5;
btn.backgroundColor?=?[UIColor?blackColor];
[btn?addTarget:self?action:@selector(btnClick)?forControlEvents:UIControlEventTouchUpInside];
[self.view?addSubview:btn];
}
-?(void)btnClick
{
Sub1ViewController?*?sub1?=?[[Sub1ViewController?alloc]init];
sub1.view.backgroundColor?=?[UIColor?blueColor];
sub1.delegate?=?self;
[self?presentViewController:sub1?animated:YES?completion:nil];
}
-?(void)changeColor:(UIColor?*)color
{
self.view.backgroundColor?=?color;
}
子視圖控制器
//.h文件
#import?
@protocol?Sub1ViewControllerDelete?
-?(void)changeColor:(UIColor?*)color;
@end
@interface?Sub1ViewController?:?UIViewController
@property?(assign,nonatomic,readwrite)id?delegate;
@end
//.m文件
-?(void)viewDidLoad
{
[super?viewDidLoad];
[self?createPopToRootViewBtn];
}
-?(void)createPopToRootViewBtn
{
UIButton?*?btn?=?[UIButton?buttonWithType:UIButtonTypeCustom];
btn.frame?=?CGRectMake(10,?30,?300,?40);
[btn?setTitle:@"進入根視圖控制器"forState:UIControlStateNormal];
btn.layer.cornerRadius?=?5;
btn.backgroundColor?=?[UIColor?blackColor];
[btn?addTarget:self?action:@selector(btnClick)?forControlEvents:UIControlEventTouchUpInside];
[self.view?addSubview:btn];
}
-?(void)btnClick
{
[_delegate?changeColor:[UIColor?orangeColor]];
[self?dismissViewControllerAnimated:YES?completion:nil];
}
方法一:通知中心
發(fā)布通知消息postNotificationName消息的名字userInfo傳的參數(shù)可有可無
NSDictionary*dic = [[NSDictionaryalloc]initWithObjectsAndKeys:numStr,@"messageNumber",nil];
[[NSNotificationCenterdefaultCenter]postNotificationName:@"message"object:nil userInfo:dic];
接收通知中心消息? @selector(myMessage:)調用的方法
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(myMessage:)name:@"message"object:nil];
- (void)registUserName:(NSNotification*)userName
{
NSLog(@"username%@",[userName.userInfoobjectForKey:@"userName"]);
loginLabel.text= [userName.userInfoobjectForKey:@"userName"];
}