通常我們在 iOS 中發(fā)生什么事件時該做什么是由 Delegate 實現(xiàn)的晒喷,例如 View 加載完后會觸發(fā) viewDidLoad。 Apple 還為我們提供了另一種通知響應(yīng)方式镜遣,那就是 NSNotification,系統(tǒng)中(UIKeyboardDidShowNotification 等) 以及某些第三方組件(例如 ASIHTTPRequest 的 kReachabilityChangedNotification 等)痛阻。
NSNotificationCenter 較之于 Delegate 可以實現(xiàn)更大的跨度的通信機(jī)制靖秩,可以為兩個無引用關(guān)系的兩個對象進(jìn)行通信。NSNotificationCenter 的通信原理使用了觀察者模式:
- NSNotificationCenter 注冊觀察者對某個事件(以字符串命名)感興趣鱼响,及該事件觸發(fā)時該執(zhí)行的 Selector 或 Block
- NSNotificationCenter 在某個時機(jī)激發(fā)事件(以字符串命名)
-
觀察者在收到感興趣的事件時鸣剪,執(zhí)行相應(yīng)的 Selector 或 Block
使用 NSNotificationCenter 的步驟示例代碼:
- 定義一個事件到來時該執(zhí)行的方法:
- (void)execute:(NSNotification *)notification {
//do something when received notification
//notification.name is @\"NOTIFICATION_NAME\"
if(notification.object && [notification.object isKindOfClass:[Test class]]){
//do something
}
}
- 注冊觀察者:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(execute:)
name:@\"NOTIFICATION_NAME\"
object:nil];
使用默認(rèn)的通知中心,上面代碼的意義的丈积,觀察者 self 在收到名為 @"NOTIFICATION_NAME" 的事件是執(zhí)行 @selector(execute:)筐骇,最后一個參數(shù)是表示會對哪個發(fā)送者對象發(fā)出的事件作出響應(yīng),nil 時表示接受所有發(fā)送者的事件江滨。
還有一種注冊觀察者的方式是用方法:
- (id)addObserverForName:(NSString*)name object:(id)obj queue:(NSOperationQueue*)queue usingBlock:(void (^)(NSNotification *))block
- 激發(fā)事件铛纬,即通知相應(yīng)的觀察者
[[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 參數(shù)對應(yīng)到方法 - (void)execute:(NSNotification *)notification 里的 notification.object, name 就是 notification.name唬滑。
代碼到這里告唆,方法 - (void)execute:(NSNotification *)notification 就會得到執(zhí)行了棺弊。
說明:我們上面用的 [NSNotificationCenter defaultCenter] 同一個實例,你也可以使用自己的 NSNotificationCenter擒悬,如:
NSNotificationCenter *notificationCenter = [[NSNotificationCenter alloc]init];
事件只會在當(dāng)前的 NotificationCenter 中廣播模她,不同的 NotificationCenter 之間的事件通知互不相干。
NSNotificationCenter 比之 Delegate 的好處就是事件發(fā)出者與響應(yīng)者可以完全不認(rèn)識懂牧,例如你在某個類中注冊了 A 觀察者某 E 事件的響應(yīng)侈净,你可以在程序的任何代碼 X 中激發(fā) E,A 的相應(yīng)選擇器即被觸發(fā)僧凤,對象 A 與 X 完全是松散的畜侦。
最后,你的觀察者如果對一些事件沒興趣了拼弃,應(yīng)該從 NotificationCenter 中移除掉
[[NSNotificationCenter defaultCenter] removeObserver:self name:@\"NOTIFICATION_NAME\"
object:test];//object 與注冊時相同
//或[[NSNotificationCenter defaultCenter] removeObserver:self];
系統(tǒng)里定義了許多的 XxxNotification 名稱夏伊,其實只要 Cmd+Shift+O 打開 Open Quickly,輸入 nsnotification 或者 uinotification 可以看到許多以 Notification 結(jié)尾的變量定義吻氧,由變量名稱也能理解在什么時候會激發(fā)什么事件溺忧,一般都是向 [NSNotificationCenter defaultCenter] 通知的。
比如你想對系統(tǒng)的某些事件時作出響應(yīng)只要注冊一個觀察者即可盯孙,想要在每次鍵盤顯示后得到通知就得關(guān)心 UIKeyboardDidShowNotification 事件:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil]
鍵盤顯示后就會執(zhí)行 self 的 keyboardDidShow 方法鲁森。