通知中心

  1. 获取通知中心,注册一个观察者和事件
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    1. 在通知中心中, 添加一个观察者和观察者事件
      [center addObserver:self selector:@selector(receiveNotification:) name:@”放学” object:nil];
      // 参数1:负责响应事件的观察对象
      // 参数2:一旦收到消息,观察者要执行的方法
      // 参数3:观察者要监听的事件
      // 参数4:可以限定消息发出者
  2. 收到通知中心的消息时 观察者(self)要调用的方法
    • (void)receiveNotification:(NSNotification *)noti
      {
      // 4,
      NSLog(@”%@”, noti.object);
      self.view.backgroundColor = [UIColor orangeColor];
      }
    • (void)ClichButton:(UIButton *)button
      {
      // 3, 通知中心的使用
      // 发送一个消息
      NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
      // 参数1:发送消息的事件名(必须一致)
      // 参数2:可以使用这个参数,传递一个对象给观察者
      // 参数3:一些消息的参数信息(系统用得较多)
      [center postNotificationName:@”放学” object:@”钥匙” userInfo:nil];
      [self.navigationController popToRootViewControllerAnimated:YES];
      }
  3. 只要注册一个观察者 一定要在类的dealloc方法中 移除掉自己的观察者身份
    在ARC下一样
    • (void)dealloc
      {
      NSNotificationCenter *center = [NSNotificationCenter defautCenter];
      [Center removeObject:self];
      [super dealloc];
      }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值