消息中心NSNotificationCenter理解

NSNotificationCenter 是 Cococa消息中心,统一管理单进程内不同线程的消息通迅,其职责只有两个:
1,提供“观查者们”对感兴趣消息的监听注册
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationName:) name: @"NotificationName" object:nil];  
 a, defaultCenter,消息中心只有一个,通过类方法获取它的单例。 
 b, addObserver,添加监听者实例,此处为当前实例
 c, selector,observer中的一个方法指针,当有消息的时候会执行此方法,并把相关上下文以参数传递过去
 d, name,注册所关心消息的名称,
 e, object,这个是对消息发送方的一个过滤,此参数据说明当前监听器仅对某个对象发出的消息感兴趣。


 整体意思:向消息中心中注册一个“监听者”(当前实例self, 相当于Java里的this)。当有名为NSWindowDidBecomeMainNotification 的消息发送到消息中心时,执行本实例的aWindowBecameMain方法。

2,接收“消息”,并把消息发送给关心它的“观查者们”。

消息的推送:
[[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationName" object:nil userInfo: [NSDictionary dictionaryWithObject:@"apple" forKey:@"name"]];  


a, postNotificationName,推送消息的名称,匹配在注册消息监听者时的消息名称。
b, object, 发送消息的实例
c, userInfo,发送消息时附带的消息上下文,此实例为一个字典实例(相当于Java里的HashMap)。


3,当有消息推送到消息中心后,会把此消息发送给相关的“监听者”,并会执行消息注册时的方法:
-(void)notificationName:(id)sender
{  
 NSString *name = [[sender userInfo] objectForKey:@"name"];  
 NSLog(@"name: %@",name);  
}

方法很简单,从消息上下文中(发送消息时的 userInfo),获取"name"并打印。 以下是一个完整的消息分发工程,
特意把事件注册与推送写到两个类中(从头文件中可以发现两个类并没有直接的引用)
主要代码如下 
notificationAppDelegate中:
//在消息中心中注册消息  
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addObserverBtn:) name:@"addObserverBtn" object:nil];  
View中:
//向消息中心推荐送名为"addObserverBtn"的消息  
[[NSNotificationCenter defaultCenter] postNotificationName:@"addObserverBtn"  
object:nil userInfo:[NSDictionary dictionaryWithObject:@"apple" forKey:@"name"]];  
在 Design Patterns 中的 Observer Pattern 
主要目是用来解决一对多的物件之间的依附关系,只要物件状态一有变动,就可以通知其他相依物件做跟更新的动作,举个简单的例子 Observer 就像是一个班级里负责联络的窗口一样,当班级内有人有通知需要通知给所有人时,只要告诉这个联络窗口,之后就由联络窗口统一通知班级内的所有人,当然也包含发佈消息的这个人。在 Objective-C 里我们并不需要真的去实作出 Observer Pattern,透过使用 NSNotificationCenter 也可以达到相同的效果。
NSNotificationCenter 可以分成三部份来看,分别是註册(订阅)ˋ註销(取消订阅)与通知通知。
 在订阅的部份,物件必须向 NSNotificationCenter 进行註册,并且说明想要订阅的通知事件,和设定收到通知时要执行的函式,通常我们可以将订阅的部份直接写在物件初始化的地方,这样物件在建立之后就可以立刻向 NSNotificationCenter 订阅他所需要的资讯
- (id)init 
{  
 self = [super init];  
 if (self)
 {   
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(echoNotification:) name:@"showSomething" object:nil];  
}  
 return self;  
}  

上述程式码在完成初始化之后随即向 NSNotificationCenter 订阅关于 showSomething 的通知事件,可以解释成日后只要有 NSNotificationCenter 发佈有关 showSomething 通知事件的通知,此物件就会执行 echoNotification: 函式,而此物件内的 echoNotification: 函式如下。
- (void)echoNotification:(NSNotification *)notification 
{  
 //取得由NSNotificationCenter送来的通知  
 NSArray *anyArray = [notification object];  
 NSLog(@"%@", [anyArray componentsJoinedByString:@"\n"]);
}  

由 NSNotificationCenter 送来的通知可以是任何形态,在这里假定订阅的 showSomething 通知事件只会送来 NSArray 形态的参数。
在取消订阅的部份,可以参考下列程式码来註销该物件对 NSNotificationCenter 订阅的所有通知事件。
[[NSNotificationCenter defaultCenter] removeObserver:self];  

 或是使用下列程式码对特定的通知事件取消订阅(同一个对象可以同时订阅数种不同的通知事件)。
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"showSomething" object:nil];  
 
最后是通知通知的部份,其程式码如下。
[[NSNotificationCenter defaultCenter] postNotificationName:@"showSomething" object:stringArray]; 
 执行上述程式码,NSNotificationCenter 将会通知所有订阅 showSomething 事件的对象,并附带通知内容(stringArray 参数)。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值