您的位置:首页 > 其它

NSNotificationCenter 的使用详解

2015-06-21 10:08 239 查看
原文地址:http://www.cnblogs.com/wenxp2006/articles/2499330.html

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

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

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

2. NSNotificationCenter 在某个时机激发事件(以字符串命名)

3. 观察者在收到感兴趣的事件时,执行相应的 Selector 或 Block



使用 NSNotificationCenter 的步骤示例代码:

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



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





2. 注册观察者:

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


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

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

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

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



[[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 中移除掉:

[[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 事件:

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


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

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

Cocoa 给我们另一种事件通知模型就是 KVO(Key-Value Obsering),基于 NSKeyValueObserving 非正式协议。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: