页面A和页面B之间的传值,如果在B页面中有一个按钮实现点击按钮pop到A页面并改变A页面的背景颜色
1.代理传值
在B页面中定义一个协议,和一个弱引用的delegate属性
A页面遵守此协议,设置代理,并实现协议中的方法
代码,A页面:
@interface ViewController ()<NewViewControllerProtocol>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"页面A";
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NewViewController *vc = [[NewViewController alloc]init];
//设置代理
vc.delegate = self;
[self.navigationController pushViewController:vc animated:YES];
}
//实现代理方法
- (void)newViewController:(NewViewController *)newViewController changeColor:(UIColor *)changeColor{
self.view.backgroundColor = changeColor;
}
B页面:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//跳回到上一个页面
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 150, 100, 50)];
[btn setTitle:@"返回" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnDidClock) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
//按钮响应事件
-(void)btnDidClock{
[self.navigationController popViewControllerAnimated:YES];
//代理传值
if ([self.delegate respondsToSelector:@selector(newViewController:changeColor:)]) {
[self.delegate newViewController:self changeColor:[UIColor redColor]];
}
}
2.block传值
在B页面中定义一个block,在需要的时候执行代码块
在A中调用block,准备要执行的代码
B页面:
@interface NewViewController : UIViewController
@property(nonatomic,copy) void(^myBlock)(UIColor *changeColor);
@end
@implementation NewViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//跳回到上一个页面
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 150, 100, 50)];
[btn setTitle:@"返回" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnDidClock) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
//按钮响应事件
-(void)btnDidClock{
[self.navigationController popViewControllerAnimated:YES];
if (self.myBlock) {
self.myBlock([UIColor redColor]);
}
}
A页面:
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"页面A";
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NewViewController *vc = [[NewViewController alloc]init];
//设置代理
vc.myBlock = ^(UIColor *changeColor){
self.view.backgroundColor = changeColor;
};
[self.navigationController pushViewController:vc animated:YES];
}
3.NSNotification传值
B页面注册通知
A页面监听通知,并触发相应的方法
A页面移除通知
B页面:
@implementation NewViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//跳回到上一个页面
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 150, 100, 50)];
[btn setTitle:@"返回" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnDidClock) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
//按钮响应事件
-(void)btnDidClock{
[self.navigationController popViewControllerAnimated:YES];
//发送通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:nil userInfo:@{@"newColor":[UIColor redColor]}];}
@end
A页面
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"页面A";
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NewViewController *vc = [[NewViewController alloc]init];
//监听通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeColor:) name:@"changeColor" object:nil];
[self.navigationController pushViewController:vc animated:YES];
}
-(void)changeColor:(NSNotification *)notice{
self.view.backgroundColor = notice.userInfo[@"newColor"];
}
-(void)dealloc{
//移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self];
}