开发者

Do I need to remove an observer from the NSNotificationCenter once, or as many times as it was added?

开发者 https://www.devze.com 2023-03-31 12:55 出处:网络
in my viewDidLoad, I add my controller as an observer for two notifications: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:NetworkStatusChangedNo

in my viewDidLoad, I add my controller as an observer for two notifications:

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

in my dealloc, should I remove it once, or twice? The removeObserver method doesn't seem t开发者_运维知识库o specify a particular notification.

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


You need to remove it only once.

If you need it, you can also use -removeObserver:name:object: to stop observing just one of the notifications.


Documentation is the best way to clear your doubts:

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

[[NSNotificationCenter defaultCenter] removeObserver:someObserver];


From Reference:

RemoveObserver: Removes all the entries specifying a given observer from the receiver’s dispatch table.

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html

so you need to call it only once

0

精彩评论

暂无评论...
验证码 换一张
取 消