轉載自http://unmi.cc/nsnotificationcenter-usage,作者“隔葉黃鶯”
通常我們在 iOS 中發生什么事件時該做什么是由 Delegate 實現的,例如 View 加載完后會觸發 viewDidLoad。Apple 還為我們提供了另一種通知響應方式,那就是 NSNotification,系統中(UIKeyboardDidShowNotification 等) 以及某些第三方組件(例如 ASIHTTPRequest 的 kReachabilityChangedNotification 等)。
NSNotificationCenter 較之於 Delegate 可以實現更大的跨度的通信機制,可以為兩個無引用關系的兩個對象進行通信。NSNotificationCenter 的通信原理使用了觀察者模式:
1. NSNotificationCenter 注冊觀察者對某個事件(以字符串命名)感興趣,及該事件觸發時該執行的 Selector 或 Block
2. NSNotificationCenter 在某個時機激發事件(以字符串命名)
3. 觀察者在收到感興趣的事件時,執行相應的 Selector 或 Block
使用 NSNotificationCenter 的步驟示例代碼:
1. 定義一個事件到來時該執行的方法:
1 - (void)execute:(NSNotification *)notification { 2 //do something when received notification 3 //notification.name is @"NOTIFICATION_NAME"
4 if(notification.object && [notification.object isKindOfClass:[Test class]]){ 5 //do something
6 } 7 }
2. 注冊觀察者:
1 [[NSNotificationCenter defaultCenter] addObserver:self 2 selector:@selector(execute:) 3 name:@"NOTIFICATION_NAME"
4 object:nil];
使用默認的通知中心,上面代碼的意義的,觀察者 self 在收到名為 @"NOTIFICATION_NAME" 的事件是執行 @selector(execute:),最后一個參數是表示會對哪個發送者對象發出的事件作出響應,nil 時表示接受所有發送者的事件。
還有一種注冊觀察者的方式是用方法:
- (id)addObserverForName:(NSString *)name object:(id)obj queue:(NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *))block
3. 激發事件,即通知相應的觀察者
[[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFICATION_NAME"
object:nil]; //或者用下面幾行代碼,明確的 notification 示例
Test *test = [[Test alloc] init]; NSNotification *notification = [NSNotification notificationWithName:@"NOTIFICATION_NAME"
object:test]; [[NSNotificationCenter defaultCenter] postNotification:notification];
這里的 object 參數對應到方法 - (void)execute:(NSNotification *)notification 里的 notification.object, name 就是 notification.name。
代碼到這里,方法 - (void)execute:(NSNotification *)notification 就會得到執行了。
說明:我們上面用的 [NSNotificationCenter defaultCenter] 同一個實例,你也可以使用自己的 NSNotificationCenter,如:
NSNotificationCenter *notificationCenter = [[NSNotificationCenter alloc]init];
事件只會在當前的 NotificationCenter 中廣播,不同的 NotificationCenter 之間的事件通知互不相干。
NSNotificationCenter 比之 Delegate 的好處就是事件發出者與響應者可以完全不認識,例如你在某個類中注冊了 A 觀察者某 E 事件的響應,你可以在程序的任何代碼 X 中激發 E,A 的相應選擇器即被觸發,對象 A 與 X 完全是松散的。
最后,你的觀察者如果對一些事件沒興趣了,應該從 NotificationCenter 中移除掉:
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"NOTIFICATION_NAME"
object:test];//object 與注冊時相同 //或[[NSNotificationCenter defaultCenter] removeObserver:self];
系統里定義了許多的 XxxNotification 名稱,其實只要 Cmd+Shift+O 打開 Open Quickly,輸入 nsnotification 或者 uinotification 可以看到許多以 Notification 結尾的變量定義,由變量名稱也能理解在什么時候會激發什么事件,一般都是向 [NSNotificationCenter defaultCenter] 通知的。
比如你想對系統的某些事件時作出響應只要注冊一個觀察者即可,想要在每次鍵盤顯示后得到通知就得關心 UIKeyboardDidShowNotification 事件:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
鍵盤顯示后就會執行 self 的 keyboardDidShow 方法。
我們可以多看看第三方 Objective-C 庫是怎么運用的 NSNotificationCenter。
Cocoa 給我們另一種事件通知模型就是 KVO(Key-Value Obsering),基於 NSKeyValueObserving 非正式協議。
參考:1. NSNotification Class Reference
2. NSNotificationCenter 的使用
3. iPhone之NSNotificationCenter使用方法
4. Key-Value Observing Programming Guide
5. AppDelegate VS. NSNotificationCenter