Abhinav Abhinav - 1 month ago 11
Swift Question

Removing all notification observer from a single place

I want to remove a notification observer and I am using the method:

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 centralized place. Can I pass 'nil' in first parameter and it will remove all observers who are listening to myNotification?

Answer

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.

[[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.

Comments