NSNotificationCenter 是 Cococa消息中心,統一管理單進程內不一樣線程的消息通迅,其職責只有兩個:ide
1,提供「觀查者們」對感興趣消息的監聽註冊post
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(printName:) name: @"messageName" object:nil]; spa
a, defaultCenter,消息中心只有一個,經過類方法獲取它的單例。.net
b, addObserver,添加監聽者實例,此處爲當前實例線程
c, selector,observer中的一個方法指針,當有消息的時候會執行此方法,並把相關上下文以參數傳遞過去指針
d, name,註冊所關心消息的名稱,component
e, object,這個是對消息發送方的一個過濾,此參數聽說明當前監聽器僅對某個對象發出的消息感興趣。orm
總體意思:server
向消息中心中註冊一個「監聽者」(當前實例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"並打印。
如下是一個完整的消息分發工程,
特地把事件註冊與推送寫到兩個類中(從頭文件中能夠發現兩個類並無直接的引用)
項目:Archive.zip
主要代碼以下
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:@"\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 參數)。