ios 从一个地方删除所有通知观察者
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5624975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Removing all notification observer from a single place
提问by Abhinav
I want to remove a notification observer and I am using the method:
我想删除一个通知观察者,我正在使用该方法:
[[NSNotificationCenter defaultCenter] removeObserver: name:@"myNotification" object:nil];
for this. Now there are many observers who are listening to this notification and I want to remove all of them in one shot from a centralised place. Can I pass 'nil' in first parameter and it will remove all observers who are listening to myNotification?
为了这。现在有很多观察者在听这个通知,我想从一个集中的地方一次性删除所有这些。我可以在第一个参数中传递“nil”,它会删除所有正在收听 myNotification 的观察者吗?
回答by Nathan Jones
You can remove an object from the notification center all together which means no notifications will get triggered. For example, when I have a view controller that has registered for notifications, I include this line in my dealloc.
您可以从通知中心一起删除一个对象,这意味着不会触发任何通知。例如,当我有一个注册了通知的视图控制器时,我将这一行包含在我的 dealloc 中。
[[NSNotificationCenter defaultCenter] removeObserver:self];
This is at the object level...so it will unregister for many notifications. It won't unregister for one notification in many objects.
这是在对象级别...因此它将取消注册许多通知。它不会取消注册多个对象中的一个通知。
Hope I understood your question correctly.
希望我正确理解了你的问题。
回答by Sruit A.Suk
In case of Swift, you doing it like this:
在 Swift 的情况下,你这样做:
NSNotificationCenter.defaultCenter().removeObserver(self)
And in Swift 3:
在 Swift 3 中:
NotificationCenter.default.removeObserver(self)
回答by XJones
Unfortunately, there is no way to remove all observers of a specific notification in one place. While there are certainly cases where this would be nice, it would be a dangerous thing to do as generally, the object doing the observing should be responsible for adding and removing itself as an observer of a particular notification. This ensures no unpredictable behavior b/c observers can come and go so they configure and clean up after themselves.
不幸的是,没有办法在一个地方删除特定通知的所有观察者。虽然在某些情况下这会很好,但通常这样做是一件危险的事情,执行观察的对象应该负责添加和删除自己作为特定通知的观察者。这确保了 b/c 观察者不会出现不可预测的行为,因此他们会自行配置和清理。
If an object that generates notifications goes away, it won't matter to the observer as the observer doesn't know about that object anyway. It just means that object won't be generating any more notifications.
如果生成通知的对象消失了,对于观察者来说无关紧要,因为无论如何观察者都不知道该对象。这只是意味着该对象不会再生成任何通知。
[EDIT: RESPONSE TO YOUR COMMENT RE CLASS B STOPPING CLASS A FROM OBSERVING]
[编辑:对您的评论的回应 B 类停止观察 A 类]
I just saw your comment. There are different ways to accomplish this, particularly if class B knows about class A. As you reference classes it sounds like you want to affect all instances of a class vs a particular instance. If you have some condition you can check when handling the notification, that's how I would approach this. In the notification handler something like:
我刚看到你的评论。有不同的方法可以实现这一点,特别是如果 B 类知道 A 类。当您引用类时,听起来您想要影响类的所有实例与特定实例。如果您有一些条件可以在处理通知时检查,这就是我将如何处理的。在通知处理程序中类似:
if ([self shouldRespondToNotificationNamed:notification.name]) {
[self performNotificationAction];
}
If you don't have a condition you can check, then create one either in the class in question as an iVar or in a place where you can access it globally for all class instances. I generally use a singleton to store global app state that doesn't persist. If it persists, then use whatever method you're using for other state.
如果你没有,你可以检查一个条件,那么在相关的类作为伊娃,或者在可以全局访问所有的类实例的地方可以创建一个。我通常使用单例来存储不持久的全局应用程序状态。如果它仍然存在,则使用您用于其他状态的任何方法。