NSNotificationCenter 的使用詳解

NSNotificationCenter 的使用詳解

通常我們在 iOS 中發生什麼事件時該做什麼是由 Delegate 實現的,例如 View 加載完後會觸發 viewDidLoad。  Apple 還爲我們提供了另一種通知響應方式,那就是 NSNotification,系統中(UIKeyboardDidShowNotification 等) 以及某些第三方組件(例如 ASIHTTPRequest 的 kReachabilityChangedNotification 等)。

NSNotificationCenter 較之於 Delegate 可以實現更大的跨度的通信機制,可以爲兩個無引用關係的兩個對象進行通信。NSNotificationCenter 的通信原理使用了觀察者模式:

1. NSNotificationCenter 註冊觀察者對某個事件(以字符串命名)感興趣,及該事件觸發時該執行的 Selector 或 Block
2. NSNotificationCenter 在某個時機激發事件(以字符串命名)
3. 觀察者在收到感興趣的事件時,執行相應的 Selector 或 Block

notificationcenter

使用 NSNotificationCenter 的步驟示例代碼:

1. 定義一個事件到來時該執行的方法:

- (void)execute:(NSNotification *)notification {
    //do something when received notification
    //notification.name is @\"NOTIFICATION_NAME\"
    if(notification.object && [notification.object isKindOfClass:[Test class]]){
        //do something
    }
}


2. 註冊觀察者:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(execute:)
                                             name:@\"NOTIFICATION_NAME\"
                                           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] 通知的。

built-in_notifications

比如你想對系統的某些事件時作出響應只要註冊一個觀察者即可,想要在每次鍵盤顯示後得到通知就得關心 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


發佈了28 篇原創文章 · 獲贊 7 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章