delegate、block和NSNotification传值

页面A和页面B之间的传值,如果在B页面中有一个按钮实现点击按钮popA页面并改变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];
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值