zoukankan      html  css  js  c++  java
  • NSNotificationCenter 的使用详解

    转载自http://unmi.cc/nsnotificationcenter-usage,作者“隔叶黄莺”

     

    通常我们在 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 非正式协议。

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

  • 相关阅读:
    LR常用函数汇总
    常用工具软件包下载地址
    MySQL分表操作的例子
    Redis性能优化之redis.cnf配置参数
    Redis监控之redis-live.conf配置
    Oracle中查询和定位数据库问题的SQL语句
    Oracle种常用性能监控SQL语句
    show processlist使用介绍
    MySQL流程控制和存储过程介绍
    MySQL字符集和排序介绍
  • 原文地址:https://www.cnblogs.com/pure/p/2469424.html
Copyright © 2011-2022 走看看