NSNotificationCenter 的使用详解

通常我们在 iOS 中发生什么事件时该做什么是由 Delegate 实现的,例如 View 加载完后会触发 viewDidLoad。  Apple 还为我们提供了另一种通知响应方式,那就是 NSNotification,系统中(UIKeyboardDidShowNotification 等) 以及某些第三方组件(例如 ASIHTTPRequest 的 kReachabilityChangedNotification 等)。

NSNotificationCenter 较之于 Delegate 可以实现更大的跨度的通信机制,可以为两个无引用关系的两个对象进行通信。NSNotificationCenter 的通信原理使用了观察者模式:

1. NSNotificationCenter 注册观察者对某个事件(以字符串命名)感兴趣,及该事件触发时该执行的 Selector 或 Block
2. NSNotificationCenter 在某个时机激发事件(以字符串命名)
3. 观察者在收到感兴趣的事件时,执行相应的 Selector 或 Block

使用 NSNotificationCenter 的步骤示例代码:

1. 定义一个事件到来时该执行的方法:

1
2
3
4
5
6
7
- ( void )execute:( NSNotification *)notification {
     //do something when received notification
     //notification.name is @"NOTIFICATION_NAME"
     if (notification.object && [notification.object isKindOfClass:[Test class ]]){
         //do something
     }
}

2. 注册观察者:

1
2
3
4
[[ NSNotificationCenter defaultCenter] addObserver: self
                                          selector: @selector (execute:)
                                              name:@ "NOTIFICATION_NAME"
                                            object: nil ];

使用默认的通知中心,上面代码的意义的,观察者 self   在收到名为 @"NOTIFICATION_NAME" 的事件是执行 @selector(execute:),最后一个参数是表示会对哪个发送者对象发出的事件作出响应,nil 时表示接受所有发送者的事件。

还有一种注册观察者的方式是用方法:

- (id)addObserverForName:(NSString *)name object:(id)obj queue:(NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *))block

3. 激发事件,即通知相应的观察者

1
2
3
4
5
6
7
[[ NSNotificationCenter defaultCenter] postNotificationName:@ "NOTIFICATION_NAME"
                                                     object: nil ];
//或者用下面几行代码,明确的 notification 示例
Test *test = [[Test alloc] init];
NSNotification *notification = [ NSNotification notificationWithName:@ "NOTIFICATION_NAME"
                                                              object:test];
[[ NSNotificationCenter defaultCenter] postNotification:notification];

这里的 object 参数对应到方法 - (void)execute:(NSNotification *)notification 里的 notification.object, name 就是 notification.name

代码到这里,方法 - (void)execute:(NSNotification *)notification 就会得到执行了。

说明:我们上面用的  [NSNotificationCenter defaultCenter] 同一个实例,你也可以使用自己的 NSNotificationCenter,如:

NSNotificationCenter *notificationCenter = [[NSNotificationCenter alloc]init];

事件只会在当前的 NotificationCenter 中广播,不同的 NotificationCenter 之间的事件通知互不相干。

NSNotificationCenter 比之 Delegate 的好处就是事件发出者与响应者可以完全不认识,例如你在某个类中注册了 A 观察者某 E 事件的响应,你可以在程序的任何代码 X 中激发 E,A 的相应选择器即被触发,对象 A 与 X 完全是松散的。

最后,你的观察者如果对一些事件没兴趣了,应该从 NotificationCenter 中移除掉:

1
2
3
[[ NSNotificationCenter defaultCenter] removeObserver: self name:@ "NOTIFICATION_NAME"
                                               object:test]; //object 与注册时相同
//或[[NSNotificationCenter defaultCenter] removeObserver:self];

系统里定义了许多的 XxxNotification 名称,其实只要 Cmd+Shift+O 打开 Open Quickly,输入 nsnotification 或者 uinotification 可以看到许多以 Notification 结尾的变量定义,由变量名称也能理解在什么时候会激发什么事件,一般都是向 [NSNotificationCenter defaultCenter] 通知的。

比如你想对系统的某些事件时作出响应只要注册一个观察者即可,想要在每次键盘显示后得到通知就得关心 UIKeyboardDidShowNotification 事件:

1
2
3
4
[[ NSNotificationCenter defaultCenter] addObserver: self
                                          selector: @selector (keyboardDidShow:)
                                              name: UIKeyboardDidShowNotification
                                            object: nil ];

键盘显示后就会执行 self 的 keyboardDidShow 方法。

我们可以多看看第三方 Objective-C 库是怎么运用的 NSNotificationCenter。

Cocoa 给我们另一种事件通知模型就是 KVO(Key-Value Obsering),基于 NSKeyValueObserving 非正式协议。

参考:1. NSNotification Class Reference
            2. NSNotificationCenter 的使用
            3. iPhone之NSNotificationCenter使用方法
            4. Key-Value Observing Programming Guide
            5. AppDelegate VS. NSNotificationCenter

本文链接 http://unmi.cc/nsnotificationcenter-usage, 来自 隔叶黄莺 Unmi Blog

[版权声明]本站内文章,如未特别注明,均系原创或翻译之作,本人 Unmi 保留一切权利。本站原创及译作未经本人许可,不得用于商业用途及传统媒体。网络媒体可随意转载,或以此为基础进行演译,但务必以链接形式注明原始出处和作者信息,否则属于侵权行为。另对本站转载他处文章,俱有说明,如有侵权请联系本人,本人将会在第一时间删除侵权文章。及此说明,重之之重。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值