ios 通知中心 NSNotificationCenter & NSNotification

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


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

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

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

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

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

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

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

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

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

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

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

Creating Notifications  
+ notificationWithName:object:  
+ notificationWithName:object:userInfo:  
Getting Notification Information  
– name  
– object  
– userInfo  

(2) NSNotificationCenter :用于发送消息

Getting the Notification Center  
+ defaultCenter  
Managing Notification Observers  
– addObserverForName:object:queue:usingBlock:  
– addObserver:selector:name:object:  
– removeObserver:  
– removeObserver:name:object:  
Posting Notifications  
– postNotification:  
– postNotificationName:object:  
– postNotificationName:object:userInfo:

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

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

Poster.h

#import <Foundation/Foundation.h>  
  
@interface Poster : NSObject  
  
-(void)postMessage;  
  
@end 

Poster.m

#import "Poster.h"  
  
@implementation Poster  
  
-(void)postMessage{  
      
    //1.下面两条语句等价  
    //二者的区别是第一条语句是直接发送消息内容,第二条语句先创建一个消息,然后再发送消息  
    [[NSNotificationCenter defaultCenter] postNotificationName:@"PosterOne" object:@"This is posterone!"];  
      
//    [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"PosterOne" object:@"This is posterone"]];  
      
    //2.下面两条语句等价  
    //参数:userInfo  --- Information about the the notification.  
    [[NSNotificationCenter defaultCenter] postNotificationName:@"PosterTwo" object:@"This is postertwo" userInfo:[NSDictionary dictionaryWithObject:@"value" forKey:@"key"]];  
      
//    [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"PosterTwo" object:@"This is postertwo" userInfo:[NSDictionary dictionaryWithObject:@"value" forKey:@"key"]]];  
}  
  
@end

Observer.h

#import <Foundation/Foundation.h>  
  
@interface Observer : NSObject  
  
-(void)observer;  
  
@end

Observer.m

#import "Observer.h"  
  
@implementation Observer  
  
-(void)observer {  
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callBack1:) name:@"PosterOne" object:nil];  
      
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callBack2:) name:@"PosterTwo" object:nil];  
      
    //删除所有的observer  
//    [[NSNotificationCenter defaultCenter] removeObserver:self];  
    //删除名字为name的observer  
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"PosterOne" object:nil];  
      
}  
  
-(void)callBack1:(NSNotification*)notification{  
    NSString *nameString = [notification name];  
    NSString *objectString = [notification object];  
    NSLog(@"name = %@,object = %@",nameString,objectString);  
}  
  
-(void)callBack2:(NSNotification*)notification{  
    NSString *nameString = [notification name];  
    NSString *objectString = [notification object];  
    NSDictionary *dictionary = [notification userInfo];  
    NSLog(@"name = %@,object = %@,userInfo = %@",nameString,objectString,[dictionary objectForKey:@"key"]);  
}  
  
@end  

main.m

#import <Foundation/Foundation.h>  
#import "Poster.h"  
#import "Observer.h"  
  
int main(int argc, const char * argv[])  
{  
  
    @autoreleasepool {  
          
        //注意这里的顺序,要先observer,然后再poster  
        Observer *myObserver = [[Observer alloc] init];  
        [myObserver observer];  
          
        Poster *poster = [[Poster alloc] init];  
        [poster postMessage];  
    }  
    return 0;  
}  


转载自:http://blog.csdn.net/crayondeng/article/details/9372079

参考:http://blog.csdn.net/dqjyong/article/details/7678108

http://www.cnblogs.com/iyou/p/3642070.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值