轉載編程
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 :用於建立傳遞的消息
- 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{
-
-
-
- [[NSNotificationCenter defaultCenter] postNotificationName:@"PosterOne" object:@"This is posterone!"];
-
-
-
-
-
- [[NSNotificationCenter defaultCenter] postNotificationName:@"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];
-
-
-
-
-
-
- }
-
- -(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 *myObserver = [[Observer alloc] init];
- [myObserver observer];
-
- Poster *poster = [[Poster alloc] init];
- [poster postMessage];
- }
- return 0;
- }
好了,大概有關的內容都差很少了吧
附:
不過有個方法
addObserverForName:object:queue:usingBlock:
還不太懂如何使用,暫時放一下,有知道了麻煩評論告訴了。