UIViewController之间的传值方式二 通知

接着昨天继续,今天使用通知实现两个控制器之间的传值, 这个过程比较简单。

要求:bViewController中textField用户输入完毕后跳转到aViewController同时要更改它的label标签显示

实现:

第一步:在bViewController消失时发送一个通知,向通知中心发送

- (void)viewWillDisappear:(BOOL)animated

{

    [super viewWillDisappear:animated];        

    [[NSNotificationCenter defaultCenter]   postNotificationName:kChangeLabelTextNotification object:_textField.text];

    

    self.block(_textField.text);

}


通知名称最好使用宏定义,以防出错

#define kChangeLabelTextNotification @"changeLabelTextNotification"


第二步:在aViewController中监听通知,可以在init中添加

- (instancetype)init

{

    if (self = [super init]) {

        

        [[NSNotificationCenter defaultCenter

                                  addObserver:self                                                                         selector:@selector(notificationAction:) 

                                         name:kChangeLabelTextNotification 

                                       object:nil];


    }

    return self;

}

实现通知的监听方法去修改label

- (void)notificationAction:(NSNotification *)notification

{

    _label.text = notification.object;

}


注意:有监听通知就要移除通知

- (void)dealloc

{

    [[NSNotificationCenter defaultCenter] removeObserver:self name:kChangeLabelTextNotification object:nil];

}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值