2012-05-01 36 views
1

我看不到蘋果的文檔https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsnotificationcenter_Class/Reference/Reference.html爲什麼NSNotification第三個參數經常是object:nil?

第三個參數對象的解釋在這樣一個How to create a class to send and receive events through NSNotificationCenter in Objective-C?它是零

沒有人可以解釋這一切的例子:爲什麼對象:無?

什麼時候不是零?

回答

4

當您需要知道哪個對象發佈通知時,不是零。看看-(void)addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(id)notificationSender

notificationSender

的對象,其通知觀察者要接收;也就是說,只有該發件人發送的通知纔會發送給觀察者。

如果您通過,通知中心不會使用通知發件人來決定是否將其發送給觀察者。

(來自同一NSNotificationCenter引用您的鏈接)

+0

觀察者如何知道notificationSender,因爲觀察者設計模式應該將觀察者與觀察者分離:) – user310291

+0

@ user310291這是一個真正的問題還是諷刺性的評論? – borrrden

+0

這當然是一個真正的問題:我如何獲得參考?這是否意味着我必須創建Observee實例或從某個控制器獲取它? – user310291

7

當你正在看某個對象是不是零。

場景:您有兩個對象,每個對象通過NSNotificationCenter觸發事件。你只想接收objectA的事件。

解決方法:通過NSNotificationCenter訂閱並傳入objectA作爲對象參數。

+0

+1我一直在想這個問題的答案:D –

相關問題