NSNotificationCenter

NSNotificationCenter 是 Cococa消息中心,统一管理单进程内不一样线程的消息通迅,其职责只有两个: post

 1,提供“观查者们”对感兴趣消息的监听注册 this

  1. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(printName:) name: @"messageName" object:nil];  
 

 a, defaultCenter,消息中心只有一个,经过类方法获取它的单例。 spa

 b, addObserver,添加监听者实例,此处为当前实例 线程

 c, selector,observer中的一个方法指针,当有消息的时候会执行此方法,并把相关上下文以参数传递过去 指针

 d, name,注册所关心消息的名称, component

 e, object,这个是对消息发送方的一个过滤,此参数听说明当前监听器仅对某个对象发出的消息感兴趣。 server

 

 总体意思: 对象

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

 

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

 

 消息的推送:

  1. [[NSNotificationCenter defaultCenter] postNotificationName:@"messageName" object:nil userInfo: [NSDictionary dictionaryWithObject:@"jory" forKey:@"name"|^Archive.zip]];  
 

 a, postNotificationName,推送消息的名称,匹配在注册消息监听者时的消息名称。

 b, object, 发送消息的实例

 c, userInfo,发送消息时附带的消息上下文,此实例为一个字典实例(至关于Java里的HashMap)。

 3,当有消息推送到消息中心后,会把此消息发送给相关的“监听者”,并会执行消息注册时的方法:

  1. -(void)printName:(id)sender{  
  2. NSString *name = [[sender userInfo] objectForKey:@"name"];  
  3. NSLog(@"name: %@",name);  
  4. }  
 

 方法很简单,从消息上下文中(发送消息时的 userInfo),获取"name"并打印。

 如下是一个完整的消息分发工程,

 特地把事件注册与推送写到两个类中(从头文件中能够发现两个类并无直接的引用)

 主要代码以下 

 notificationTestAppDelegate中:

  1. //在消息中心中注册消息  
  2. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clickBtn:) name:@"clickBtn" object:nil];  
 

 TestView中:

  1. //向消息中心推荐送名为"clickBtn"的消息  
  2. [[NSNotificationCenter defaultCenter] postNotificationName:@"clickBtn"  
  3. object:nil userInfo:[NSDictionary dictionaryWithObject:@"jory" forKey:@"name"]];  

在 Design Patterns 中的 Observer Pattern 主要目是用来解决一对多的物件之间的依附关系,只要物件状态一有变更,就能够通知其余相依物件作跟更新的动做,举个简单的例子 Observer 就像是一个班级里负责联络的窗口同样,当班级内有人有讯息须要通知给全部人时,只要告诉这个联络窗口,以后就由联络窗口统一通知班级内的全部人,固然也包含发佈消息的这我的。在 Objective-C 里咱们并不须要真的去实做出 Observer Pattern,透过使用 NSNotificationCenter 也能够达到相同的效果。

 NSNotificationCenter 能够分红三部份来看,分别是註册(订阅)ˋ註销(取消订阅)与讯息通知。

 在订阅的部份,物件必须向 NSNotificationCenter 进行註册,而且说明想要订阅的讯息事件,和设定收到通知时要执行的函式,一般咱们能够将订阅的部份直接写在物件初始化的地方,这样物件在创建以后就能够马上向 NSNotificationCenter 订阅他所须要的资讯

  1. - (id)init {  
  2.  self = [super init];  
  3.  if (self) {  
  4.   
  5.  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(echoNotification:) name:@"showSomething" object:nil];  
  6.  }  
  7.   
  8.  return self;  
  9.  }  

上述程式码在完成初始化以后随即向 NSNotificationCenter 订阅关于 showSomething 的讯息事件,能够解释成往后只要有 NSNotificationCenter 发佈有关 showSomething 讯息事件的通知,此物件就会执行 echoNotification: 函式,而此物件内的 echoNotification: 函式以下。

  1. - (void)echoNotification:(NSNotification *)notification {  
  2.   
  3.  //取得由NSNotificationCenter送来的讯息  
  4.  NSArray *anyArray = [notification object];  
  5.   
  6.  NSLog(@"%@", [anyArray componentsJoinedByString:@"\n"]);  
  7.  }  
 

由 NSNotificationCenter 送来的讯息能够是任何形态,在这里假定订阅的 showSomething 讯息事件只会送来 NSArray 形态的参数。

 

 在取消订阅的部份,能够参考下列程式码来註销该物件对 NSNotificationCenter 订阅的全部讯息事件。

  1. [[NSNotificationCenter defaultCenter] removeObserver:self];  
 

 或是使用下列程式码对特定的讯息事件取消订阅(同一个物件能够同时订阅数种不一样的讯息事件)。

  1. [[NSNotificationCenter defaultCenter] removeObserver:self name:@"showSomething" object:nil];  
 

  最后是讯息通知的部份,其程式码以下。

  1. [[NSNotificationCenter defaultCenter] postNotificationName:@"showSomething" object:stringArray];  
 

 执行上述程式码,NSNotificationCenter 将会通知全部订阅 showSomething 讯息事件的物件,并附带讯息内容(stringArray 参数)。

相关文章
相关标签/搜索