zoukankan      html  css  js  c++  java
  • iOS 通知中心 NSNotificationCenter & NSNotification

    转载

    http://blog.csdn.net/crayondeng/article/details/9372079

     通知中心是 Foundation 框架的一个子系统,它向应用程序中注册为某个事件观察者的所有对象广播消息(即通知)。(从编程角度而言,它是 NSNotificationCenter 类的实例)。该事件可以是发生在应用程序中的任何事情,例如进入后台状态,或者用户开始在文本栏中键入。通知是告诉观察者,事件已经发生或即将发生,因此让观察者有机会以合适的方式响应。通过通知中心来传播通知,是增加应用程序对象间合作和内聚力的一种途径。


    任何对象都可以观察通知,但要做到这一点,该对象必须注册,以接收通知。在注册时,它必须指定选择器,以确定由通知传送所调用的方法;方法签名必须只有一个参数:通知对象。注册后,观察者也可以指定发布对象。

    (以上是官方文档中的解释)

    ------------------------------------------华丽的分割线----------------------------------------------------------

    通知中心包括两个重要的类:

    (1)NSNotificationCenter: 实现NSNotificationCenter的原理是一个观察者模式,获得NSNotificationCenter的方法只有一种,那就是[NSNotificationCenter defaultCenter] ,通过调用静态方法defaultCenter就可以获取这个通知中心的对象了,而NSNotificationCenter是一个单例模式,而这个通知中心的对象会一直存在于一个应用的生命周期。

      (2)  NSNotification: 这是消息携带的载体,通过它,可以把消息内容传递给观察者。

     (3)一个NSNotificationCenter可以有许多的通知消息NSNotification,对于每一个NSNotification可以有很多的观察者Observer来接收通知。

    通过上面的介绍可以知道,通过通知中心也可以实现不同类之间的参数传递。

    注意当接受到消息后,不想再收到消息了,要把observer删除remove。

    下面介绍如何使用(具体解释看文档)。

    (1)NSNotification :用于创建传递的消息

    1. Creating Notifications  
    2. + notificationWithName:object:  
    3. + notificationWithName:object:userInfo:  
    4. Getting Notification Information  
    5. – name  
    6. – object  
    7. – userInfo  


    (2)NSNotificationCenter :用于发送消息

    1. Getting the Notification Center  
    2. + defaultCenter  
    3. Managing Notification Observers  
    4. – addObserverForName:object:queue:usingBlock:  
    5. – addObserver:selector:name:object:  
    6. – removeObserver:  
    7. – removeObserver:name:object:  
    8. Posting Notifications  
    9. – postNotification:  
    10. – postNotificationName:object:  
    11. – postNotificationName:object:userInfo:  

    demo(例子中基本上涉及到以上所有的方法了):

    定义了两个类:Poster(发送消息)和Observer(接受消息)

    Poster.h

    1. #import <Foundation/Foundation.h>  
    2.   
    3. @interface Poster : NSObject  
    4.   
    5. -(void)postMessage;  
    6.   
    7. @end  

    Poster.m

    1. #import "Poster.h"  
    2.   
    3. @implementation Poster  
    4.   
    5. -(void)postMessage{  
    6.       
    7.     //1.下面两条语句等价  
    8.     //二者的区别是第一条语句是之间发送消息内容,第二条语句先创建一个消息,然后再发送消息  
    9.     [[NSNotificationCenter defaultCenter] postNotificationName:@"PosterOne" object:@"This is posterone!"];  
    10.       
    11. //    [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"PosterOne" object:@"This is posterone"]];  
    12.       
    13.     //2.下面两条语句等价  
    14.     //参数:userInfo  --- Information about the the notification.  
    15.     [[NSNotificationCenter defaultCenter] postNotificationName:@"PosterTwo" object:@"This is postertwo" userInfo:[NSDictionary dictionaryWithObject:@"value" forKey:@"key"]];  
    16.       
    17. //    [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"PosterTwo" object:@"This is postertwo" userInfo:[NSDictionary dictionaryWithObject:@"value" forKey:@"key"]]];  
    18. }  
    19.   
    20. @end  

    Observer.h

    1. #import <Foundation/Foundation.h>  
    2.   
    3. @interface Observer : NSObject  
    4.   
    5. -(void)observer;  
    6.   
    7. @end  

    Observer.m

    1. #import "Observer.h"  
    2.   
    3. @implementation Observer  
    4.   
    5. -(void)observer {  
    6.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callBack1:) name:@"PosterOne" object:nil];  
    7.       
    8.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callBack2:) name:@"PosterTwo" object:nil];  
    9.       
    10.     //删除所有的observer  
    11. //    [[NSNotificationCenter defaultCenter] removeObserver:self];  
    12.     //删除名字为name的observer  
    13. //    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"PosterOne" object:nil];  
    14.       
    15. }  
    16.   
    17. -(void)callBack1:(NSNotification*)notification{  
    18.     NSString *nameString = [notification name];  
    19.     NSString *objectString = [notification object];  
    20.     NSLog(@"name = %@,object = %@",nameString,objectString);  
    21. }  
    22.   
    23. -(void)callBack2:(NSNotification*)notification{  
    24.     NSString *nameString = [notification name];  
    25.     NSString *objectString = [notification object];  
    26.     NSDictionary *dictionary = [notification userInfo];  
    27.     NSLog(@"name = %@,object = %@,userInfo = %@",nameString,objectString,[dictionary objectForKey:@"key"]);  
    28. }  
    29.   
    30. @end  

    main.m

    1. #import <Foundation/Foundation.h>  
    2. #import "Poster.h"  
    3. #import "Observer.h"  
    4.   
    5. int main(int argc, const char * argv[])  
    6. {  
    7.   
    8.     @autoreleasepool {  
    9.           
    10.         //注意这里的顺序,要先observer,然后再poster  
    11.         Observer *myObserver = [[Observer alloc] init];  
    12.         [myObserver observer];  
    13.           
    14.         Poster *poster = [[Poster alloc] init];  
    15.         [poster postMessage];  
    16.     }  
    17.     return 0;  
    18. }  

    好了,大概有关的内容都差不多了吧大笑

    附:

    不过有个方法

    addObserverForName:object:queue:usingBlock:

    还不太懂如何使用,暂时放一下,有知道了麻烦评论告诉了。
  • 相关阅读:
    函数式编程思想:不变性
    如何制作JQuery Plugin 插件【插件参数的配置】
    jQuery Mobile 中文手册 Ajax开发版——初始化部分
    转载:真实的用户,真实的中国互联网
    创建自定义的jQuery移动主题
    HTML5 LocalStorage 本地存储
    Safari调试工具
    为什么项目经理拿的钱比程序员多?
    移动浏览器项目WebApp需要jQuery吗?移动设备放弃jQuery的理由
    HTML5设计原则
  • 原文地址:https://www.cnblogs.com/ioschen/p/3311867.html
Copyright © 2011-2022 走看看