美文网首页iOS Developer
iOS系统复习(一) 通知

iOS系统复习(一) 通知

作者: lxyz22zp | 来源:发表于2016-02-24 11:07 被阅读159次

一, NSNotification (通知)

  • 通知: 是一个对象能给其他任意数量的对象广播消息的一种方式,对象之间没有耦合关系
  • NSNotification(通知),封装了要广播的信息(包括用做标签’名字’,需要发送的任意类型的’对象’,一个存放其他信息的字典)
  • NSNotificationCenter(通知中心),管理注册接收消息对象,广播消息; NSNotificationCenter是一个单例可以通过+defaultCenter方法获取,
  • Observer(观察者),需要监听通知的对象,即广播的信息的接受者
    注意:observer在结束监听时需要在通知中心注销
  • 常用方法:
    1 创建通知
    notificationWithName:object: notificationWithName:object:userInfo:.
    2 通过通知中心发送通知
    postNotificationName:object: postNotificationName:object:userInfo:
    3 添加一个监听者
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(aWindowBecameMain:) name:NSWindowDidBecomeMainNotification object:nil];
    4 移除监听者
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@”” object:nil];

相关文章

网友评论

    本文标题:iOS系统复习(一) 通知

    本文链接:https://www.haomeiwen.com/subject/wpjlcttx.html