NSNotificationCenter详解

NSNotificationCenter 是 Cococa消息中心,统一管理单进程内不同线程的消息通迅,其职责只有两个:
 1,提供“观查者们”对感兴趣消息的监听注册
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(printName:) name: @"messageName" object:nil];  
 
 a, defaultCenter,消息中心只有一个,通过类方法获取它的单例。
 b, addObserver,添加监听者实例,此处为当前实例
 c, selector,observer中的一个方法指针,当有消息的时候会执行此方法,并把相关上下文以参数传递过去
 d, name,注册所关心消息的名称,
 e, object,这个是对消息发送方的一个过滤,此参数据说明当前监听器仅对某个对象发出的消息感兴趣,nil表示监听任何发送消息对象。
 
 整体意思:
 向消息中心中注册一个“监听者”(当前实例self, 相当于Java里的this)。当有名为NSWindowDidBecomeMainNotification 的消息发送到消息中心时,执行本实例的aWindowBecameMain方法。
 
 2,接收“消息”,并把消息发送给关心它的“观查者们”。
 
 消息的推送:
[[NSNotificationCenter defaultCenter] postNotificationName:@"messageName" object:nil userInfo: [NSDictionary dictionaryWithObject:@"jory" forKey:@"name"|^Archive.zip]];  
 
 a, postNotificationName,推送消息的名称,匹配在注册消息监听者时的消息名称。
 b, object, 发送消息的实例
 c, userInfo,发送消息时附带的消息上下文,此实例为一个字典实例(相当于Java里的HashMap)。
 3,当有消息推送到消息中心后,会把此消息发送给相关的“监听者”,并会执行消息注册时的方法:
-(void)printName:(id)sender{  
NSString *name = [[sender userInfo] objectForKey:@"name"];  
NSLog(@"name: %@",name);  
}  
 
 方法很简单,从消息上下文中(发送消息时的 userInfo),获取"name"并打印。
 以下是一个完整的消息分发工程,
 特意把事件注册与推送写到两个类中(从头文件中可以发现两个类并没有直接的引用)
 主要代码如下
 notificationTestAppDelegate中:
//在消息中心中注册消息  
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clickBtn:) name:@"clickBtn" object:nil];  
 
 TestView中:
//向消息中心推荐送名为"clickBtn"的消息  
[[NSNotificationCenter defaultCenter] postNotificationName:@"clickBtn"  
object:nil userInfo:[NSDictionary dictionaryWithObject:@"jory" 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:@" "]);  
 }  
 
由 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 参数)。

 
原文地址:https://www.cnblogs.com/huangh/p/4151685.html