我是否需要从NSNotificationCenter中删除观察者一次,或者添加多少次观察者?

Sim*_*mon 2 iphone nsnotificationcenter

在我viewDidLoad,我添加我的控制器作为两个通知的观察者:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:NetworkStatusChangedNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkLocationStatus:) name:LocationStatusChangedNotification object:nil];
Run Code Online (Sandbox Code Playgroud)

在我dealloc,我应该删除一次,还是两次?removeObserver方法似乎没有指定特定通知.

[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] removeObserver:self]; // is this required?
Run Code Online (Sandbox Code Playgroud)

Cha*_*pta 5

您只需要删除一次.

如果需要,您还可以使用-removeObserver:name:object:停止观察其中一个通知.