Abh*_*nav 28 iphone cocoa-touch objective-c ios
我想删除通知观察者,我正在使用该方法:
[[NSNotificationCenter defaultCenter] removeObserver: name:@"myNotification" object:nil];
Run Code Online (Sandbox Code Playgroud)
为了这.现在有很多观察者正在听这个通知,我想从集中的地方一次性删除所有这些通知.我可以在第一个参数中传递'nil',它会删除所有正在侦听myNotification的观察者吗?
Nat*_*nes 82
您可以一起从通知中心删除对象,这意味着不会触发任何通知.例如,当我有一个已注册通知的视图控制器时,我将此行包含在我的dealloc中.
[[NSNotificationCenter defaultCenter] removeObserver:self];
Run Code Online (Sandbox Code Playgroud)
这是在对象级别...因此它将取消注册许多通知.它不会取消注册许多对象中的一个通知.
希望我能正确理解你的问题.
在Swift的情况下,你这样做:
NSNotificationCenter.defaultCenter().removeObserver(self)
Run Code Online (Sandbox Code Playgroud)
在Swift 3中:
NotificationCenter.default.removeObserver(self)
Run Code Online (Sandbox Code Playgroud)