NSNotification

通常我们在 iOS 中发生什么事件时该做什么是由 Delegate 实现的,例如 View 加载完后会触发 viewDidLoad。 Apple 还为我们提供了另一种通知响应方式,那就是 NSNotification,系统中(UIKeyboardDidShowNotification 等) 以及某些第三方组件(例如 ASIHTTPRequest 的 kReachabilityChangedNotification 等)。
NSNotificationCenter 较之于 Delegate 可以实现更大的跨度的通信机制,可以为两个无引用关系的两个对象进行通信。NSNotificationCenter 的通信原理使用了观察者模式:

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


    NSNotification_第1张图片

使用 NSNotificationCenter 的步骤示例代码:

  • 定义一个事件到来时该执行的方法:
- (void)execute:(NSNotification *)notification {
    //do something when received notification
    //notification.name is @\"NOTIFICATION_NAME\"
    if(notification.object && [notification.object isKindOfClass:[Test class]]){
        //do something
    }
}
  • 注册观察者:
[[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
  • 激发事件,即通知相应的观察者
[[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] 通知的。


NSNotification_第2张图片

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

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

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

你可能感兴趣的:(NSNotification)