2011-08-26 71 views
2

在我viewDidLoad,添加我的控制器作爲觀察員兩個通知:我需要從NSNotificationCenter中刪除觀察者一次,還是一次添加觀察者?

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:NetworkStatusChangedNotification object:nil]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkLocationStatus:) name:LocationStatusChangedNotification object:nil]; 

在我dealloc,應該有一次,我將其刪除,或兩次? removeObserver方法似乎沒有指定特定的通知。

[[NSNotificationCenter defaultCenter] removeObserver:self]; 
[[NSNotificationCenter defaultCenter] removeObserver:self]; // is this required? 

回答

5

您只需將其移除一次即可。

如果您需要它,還可以使用-removeObserver:name:object:停止僅觀察其中一個通知。

+1

您還可以刪除觀察者特定通知, /或使用'removeObserver:name:object:'的對象。如果觀察者觀察到其他通知,這將使觀察者保持在通知中心。 – Jasarien

+0

@Jasarien我認爲你已經添加了評論,並且我編輯了我的答案,以便同時說出同樣的內容:) –

+0

偉大的思想家們都認爲:P – Jasarien

2

Documentation是最好的方式來清除你的疑慮:

The following example illustrates how to unregister someObserver for all 
notifications for which it had previously registered: 

[[NSNotificationCenter defaultCenter] removeObserver:someObserver];