iOS 通知中心 NSNotificationCenter & NSNotification

轉載編程

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

 

 通知中心是 Foundation 框架的一個子系統,它嚮應用程序中註冊爲某個事件觀察者的全部對象廣播消息(即通知)。(從編程角度而言,它是 NSNotificationCenter 類的實例)。該事件能夠是發生在應用程序中的任何事情,例如進入後臺狀態,或者用戶開始在文本欄中鍵入。通知是告訴觀察者,事件已經發生或即將發生,所以讓觀察者有機會以合適的方式響應。經過通知中心來傳播通知,是增長應用程序對象間合做和內聚力的一種途徑。 post


任何對象均可以觀察通知,但要作到這一點,該對象必須註冊,以接收通知。在註冊時,它必須指定選擇器,以肯定由通知傳送所調用的方法;方法簽名必須只有一個參數:通知對象。註冊後,觀察者也能夠指定發佈對象。spa

(以上是官方文檔中的解釋).net

------------------------------------------華麗的分割線----------------------------------------------------------code

通知中心包括兩個重要的類:orm

(1)NSNotificationCenter: 實現NSNotificationCenter的原理是一個觀察者模式,得到NSNotificationCenter的方法只有一種,那就是[NSNotificationCenter defaultCenter] ,經過調用靜態方法defaultCenter就能夠獲取這個通知中心的對象了,而NSNotificationCenter是一個單例模式,而這個通知中心的對象會一直存在於一個應用的生命週期。server

  (2)  NSNotification: 這是消息攜帶的載體,經過它,能夠把消息內容傳遞給觀察者。對象

 (3)一個NSNotificationCenter能夠有許多的通知消息NSNotification,對於每個NSNotification能夠有不少的觀察者Observer來接收通知。blog

經過上面的介紹能夠知道,經過通知中心也能夠實現不一樣類之間的參數傳遞。

注意當接受到消息後,不想再收到消息了,要把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:

還不太懂如何使用,暫時放一下,有知道了麻煩評論告訴了。
相關文章
相關標籤/搜索