方法一:使用target-action设计模式

代码如下:(由根视图推出子视图,再由子视图推出根视图,在推出根视图时,子视图传一个color的属性给根视图,用来修改根视图的背景颜色)

根视图控制器代码:

//.m文件-(void)viewDidLoad{[superviewDidLoad];self.view.backgroundColor=[UIColorredColor];[selfcreateButton];}-(void)createButton{UIButton*btn=[UIButtonbuttonWithType:UIButtonTypeCustom];btn.frame=CGRectMake(10,30,300,40);[btnsetTitle:@"进入下一个视图控制器"forState:UIControlStateNormal];btn.layer.cornerRadius=5;btn.backgroundColor=[UIColorblackColor];[btnaddTarget:selfaction:@selector(btnClick)forControlEvents:UIControlEventTouchUpInside];[self.viewaddSubview:btn];}-(void)btnClick{Sub1ViewController*sub1=[[Sub1ViewControlleralloc]init];sub1.view.backgroundColor=[UIColorblueColor];sub1.target=self;sub1.action=@selector(changeColor:);[selfpresentViewController:sub1animated:YEScompletion:nil];}-(void)changeColor:(UIColor*)color{self.view.backgroundColor=color;}

子视图代码:

//.h文件@interfaceSub1ViewController:UIViewController@property(assign,readwrite,nonatomic)idtarget;@property(assign,readwrite,nonatomic)SELaction;@end//.m文件-(void)viewDidLoad{[superviewDidLoad];[selfcreatePopToRootViewBtn];}-(void)createPopToRootViewBtn{UIButton*btn=[UIButtonbuttonWithType:UIButtonTypeCustom];btn.frame=CGRectMake(10,30,300,40);[btnsetTitle:@"进入根视图控制器"forState:UIControlStateNormal];btn.layer.cornerRadius=5;btn.backgroundColor=[UIColorblackColor];[btnaddTarget:selfaction:@selector(btnClick)forControlEvents:UIControlEventTouchUpInside];[self.viewaddSubview:btn];}-(void)btnClick{if([_targetrespondsToSelector:_action]){[_targetperformSelector:_actionwithObject:[UIColororangeColor]];}[selfdismissViewControllerAnimated:YEScompletion:nil];}

方法二:通知

//.m根视图-(void)viewDidLoad{[superviewDidLoad];self.view.backgroundColor=[UIColorredColor];[selfcreateButton];[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(changeColor:)name:@"ChangeColor"object:nil];}-(void)createButton{UIButton*btn=[UIButtonbuttonWithType:UIButtonTypeCustom];btn.frame=CGRectMake(10,30,300,40);[btnsetTitle:@"进入下一个视图控制器"forState:UIControlStateNormal];btn.layer.cornerRadius=5;btn.backgroundColor=[UIColorblackColor];[btnaddTarget:selfaction:@selector(btnClick)forControlEvents:UIControlEventTouchUpInside];[self.viewaddSubview:btn];}-(void)btnClick{Sub1ViewController*sub1=[[Sub1ViewControlleralloc]init];sub1.view.backgroundColor=[UIColorblueColor];[selfpresentViewController:sub1animated:YEScompletion:nil];}-(void)changeColor:(NSNotification*)nofi{self.view.backgroundColor=[nofi.userInfoobjectForKey:@"color"];}

子视图代码

-(void)viewDidLoad{[superviewDidLoad];[selfcreatePopToRootViewBtn];}-(void)createPopToRootViewBtn{UIButton*btn=[UIButtonbuttonWithType:UIButtonTypeCustom];btn.frame=CGRectMake(10,30,300,40);[btnsetTitle:@"进入根视图控制器"forState:UIControlStateNormal];btn.layer.cornerRadius=5;btn.backgroundColor=[UIColorblackColor];[[NSNotificationCenterdefaultCenter]postNotificationName:@"ChangeColor"object:selfuserInfo:[NSDictionarydictionaryWithObjectsAndKeys:[UIColororangeColor],@"color",nil]];[btnaddTarget:selfaction:@selector(btnClick)forControlEvents:UIControlEventTouchUpInside];[self.viewaddSubview:btn];}-(void)btnClick{[selfdismissViewControllerAnimated:YEScompletion:nil];}

方法三:代码块

根视图代码:

-(void)viewDidLoad{[superviewDidLoad];self.view.backgroundColor=[UIColorredColor];[selfcreateButton];}-(void)createButton{UIButton*btn=[UIButtonbuttonWithType:UIButtonTypeCustom];btn.frame=CGRectMake(10,30,300,40);[btnsetTitle:@"进入下一个视图控制器"forState:UIControlStateNormal];btn.layer.cornerRadius=5;btn.backgroundColor=[UIColorblackColor];[btnaddTarget:selfaction:@selector(btnClick)forControlEvents:UIControlEventTouchUpInside];[self.viewaddSubview:btn];}-(void)btnClick{Sub1ViewController*sub1=[[Sub1ViewControlleralloc]init];sub1.view.backgroundColor=[UIColorblueColor];sub1.MyBlock=^(UIColor*color){self.view.backgroundColor=color;};[selfpresentViewController:sub1animated:YEScompletion:nil];}

子视图代码

//.h文件@interfaceSub1ViewController:UIViewController@property(copy,nonatomic,readwrite)void(^MyBlock)(UIColor*color);@end//.m文件-(void)viewDidLoad{[superviewDidLoad];[selfcreatePopToRootViewBtn];}-(void)createPopToRootViewBtn{UIButton*btn=[UIButtonbuttonWithType:UIButtonTypeCustom];btn.frame=CGRectMake(10,30,300,40);[btnsetTitle:@"进入根视图控制器"forState:UIControlStateNormal];btn.layer.cornerRadius=5;btn.backgroundColor=[UIColorblackColor];[btnaddTarget:selfaction:@selector(btnClick)forControlEvents:UIControlEventTouchUpInside];[self.viewaddSubview:btn];}-(void)btnClick{_MyBlock([UIColororangeColor]);[selfdismissViewControllerAnimated:YEScompletion:nil];}

方法四:代理-协议

根视图代码

//.h#import<UIKit/UIKit.h>#import"Sub1ViewController.h"@interfaceViewController:UIViewController<Sub1ViewControllerDelete>@end//.m-(void)viewDidLoad{[superviewDidLoad];self.view.backgroundColor=[UIColorredColor];[selfcreateButton];}-(void)createButton{UIButton*btn=[UIButtonbuttonWithType:UIButtonTypeCustom];btn.frame=CGRectMake(10,30,300,40);[btnsetTitle:@"进入下一个视图控制器"forState:UIControlStateNormal];btn.layer.cornerRadius=5;btn.backgroundColor=[UIColorblackColor];[btnaddTarget:selfaction:@selector(btnClick)forControlEvents:UIControlEventTouchUpInside];[self.viewaddSubview:btn];}-(void)btnClick{Sub1ViewController*sub1=[[Sub1ViewControlleralloc]init];sub1.view.backgroundColor=[UIColorblueColor];sub1.delegate=self;[selfpresentViewController:sub1animated:YEScompletion:nil];}-(void)changeColor:(UIColor*)color{self.view.backgroundColor=color;}

子视图控制器

//.h文件#import<UIKit/UIKit.h>@protocolSub1ViewControllerDelete<NSObject>-(void)changeColor:(UIColor*)color;@end@interfaceSub1ViewController:UIViewController@property(assign,nonatomic,readwrite)id<Sub1ViewControllerDelete>delegate;@end//.m文件-(void)viewDidLoad{[superviewDidLoad];[selfcreatePopToRootViewBtn];}-(void)createPopToRootViewBtn{UIButton*btn=[UIButtonbuttonWithType:UIButtonTypeCustom];btn.frame=CGRectMake(10,30,300,40);[btnsetTitle:@"进入根视图控制器"forState:UIControlStateNormal];btn.layer.cornerRadius=5;btn.backgroundColor=[UIColorblackColor];[btnaddTarget:selfaction:@selector(btnClick)forControlEvents:UIControlEventTouchUpInside];[self.viewaddSubview:btn];}-(void)btnClick{[_delegatechangeColor:[UIColororangeColor]];[selfdismissViewControllerAnimated:YEScompletion:nil];}