1.創(chuàng)建一個通知對象:使用notificationWithName:object: 或者 notificationWithName:object:userInfo:
NSNotification* notification = [NSNotification notificationWithName:kImageNotificationLoadFailed(connection.imageURL)object:self userInfo:[NSDictionary dictionaryWithObjectsAndKeys:error,@"error",connection.imageURL,@"imageURL",nil]];
創(chuàng)建自己的通知并不是必須的。而是在創(chuàng)建自己的通知之前姜凄,采用NSNotificationCenter類的方法 postNotificationName:object: 和 postNotificationName:object:userInfo:更加便利的發(fā)出通知唉擂。這種情況,一般使用NSNotificationCenter的類方法defaultCenter就獲得默認的通知對象檀葛,這樣你就可以給該程序的默認通知中心發(fā)送通知了.每一個程序都有一個自己的通知中心玩祟,即NSNotificationCenter對象。該對象采用單例設(shè)計模式屿聋,采用defaultCenter方法就可以獲得唯一的NSNotificationCenter對象空扎。
注意:NSNotification對象是不可變的,因為一旦創(chuàng)建润讥,對象是不能更改的转锈。
2.注冊通知:addObserver:selector:name:object:
添加觀察者代碼:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(aWindowBecameMain:)
name:NSWindowDidBecomeMainNotification object:nil];
這里保證了self定義了aWindowBecameMain:方法。而對于一個任意的觀察者observer楚殿,不能保證其對應(yīng)的selector有aWindowBecameMain:撮慨,可采用[observer respondsToSelector:@selector(aWindowBecameMain:)]] 進行檢查。所以完整的添加觀察者過程為:
if([observer respondsToSelector:@selector(aWindowBecameMain:)]) {
[[NSNotificationCenter defaultCenter] addObserver:observer selector:@selector(aWindowBecameMain:) name:NSWindowDidBecomeMainNotification object:nil];
}
注意到addObserver:selector:name:object:不僅指定一個觀察者脆粥,指定通知中心發(fā)送給觀察者的消息砌溺,還有接收通知的名字,以及指定的對象变隔。一般來說不需要指定name和object规伐,但如果僅僅指定了一個object,觀察者將收到該對象的所有通知匣缘。例如將上面的代碼中name改為nil,那么觀察者將接收到object對象的所有消息猖闪,但是確定不了接收這些消息的順序鲜棠。如果指指定一個通知名稱,觀察者將收到它每次發(fā)出的通知培慌。例如豁陆,上面的代碼中object為nil,那么客戶對象(self)將收到任何對象發(fā)出NSWindowDidBecomeMainNotification通知吵护。如果既沒有指定指定object献联,也沒有指定name,那么該觀察者將收到所有對象的所有消息何址。
3.發(fā)送通知:postNotificationName:object:或者performSelectorOnMainThread:withObject:waitUntilDone:
例如程序可以實現(xiàn)將一個文本可以進行一系列的轉(zhuǎn)換,例如對于一個實例进胯、RTF格式轉(zhuǎn)換成ASCII格式用爪。而轉(zhuǎn)換在一個類(如Converter類)的對象中得到處理,在誠尋執(zhí)行過程中可以加入或者刪除這種轉(zhuǎn)換胁镐。而且當(dāng)添加或者刪除Converter操作時偎血,你的程序可能需要通知其他的對象,但是這些Converter對象并不需要知道被通知對象是什么盯漂,能干什么颇玷。你只需要聲明兩個通知,"ConverterAdded" 和 "ConverterRemoved"就缆,并且在某一事件發(fā)生時就發(fā)出這兩個通知帖渠。
當(dāng)一個用戶安裝或者刪除一個Converter,它將發(fā)送下面的消息給通知中心:
[[NSNotificationCenter defaultCenter]
postNotificationName:@"ConverterAdded" object:self];
或者是
[[NSNotificationCenter defaultCenter]
postNotificationName:@"ConverterRemoved" object:self];
通知中心將會區(qū)分它們對象對這些通知感興趣并且通知他們竭宰。如果除了關(guān)心觀察者的通知名稱和觀察的對象空郊,還關(guān)心其他之外的對象,那么就把之外的對象放在通知的可選字典中切揭,或者用方法postNotificationName:object:userInfo:狞甚。
而采用performSelectorOnMainThread:withObject:waitUntilDone:則是直接調(diào)用NSNotification的方法postNotification,而postNotificationName和object參數(shù)可以放到withObject的實參中廓旬。例如:
[[NSNotificationCenter defaultCenter] performSelectorOnMainThread:@selector(postNotification:) withObject:notification waitUntilDone:YES];//注意這里的notification為自定義的一個通知對象哼审,可定義為NSNotification* notification = [NSNotification notificationWithName:@"ConverterAdded"object:self];//那么它的作用與上面的一致
4.移除通知:removeObserver:和removeObserver:name:object:
其中,removeObserver:是刪除通知中心保存的調(diào)度表一個觀察者的所有入口孕豹,而removeObserver:name:object:是刪除匹配了通知中心保存的調(diào)度表中觀察者的一個入口涩盾。
[[NSNotificationCenter defaultCenter] removeObserver:observer name:nil object:self];
注意參數(shù)notificationObserver為要刪除的觀察者,一定不能置為nil励背。