0

I have an ARC enabled project

There are few observers added in viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getSipNotification:) name:@"getSipNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(syncExtensionData:) name:@"syncExtensionData" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showLocalNotification:) name:@"showLocalNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(outgoingCall:)  name:@"outgoingCall" object:nil]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playRingtone)  name:@"playRingtone" object:nil];

I want to remove all observers so I added following line in viewDidUnload

[[NSNotificationCenter defaultCenter] removeObserver:self];

Now my question is, is this remove all observers?

If not how can do it?

UPDATE

If I want to remove a single observer how can do it?

Can you help me please.

MD SHAHIDUL ISLAM
  • 14,325
  • 6
  • 82
  • 89
  • Yes it remove all observers :) – Bug Oct 03 '13 at 09:33
  • Please note that viewDidUnload: isn't a right place for removing observer in most cases. Most likely you should do it in dealloc, viewDidDissapear or something else. – wirrwarr Oct 03 '13 at 10:03

4 Answers4

2

Yes, It will remove all observers.

 [[NSNotificationCenter defaultCenter] removeObserver:self];

And you can remove a particular observer like this...

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"syncExtensionData" object:nil];
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
2

In my application i used this notification :

for particular observer remove this way :

 -(void)viewWillAppear:(BOOL)animated
 {

   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceRotatedFeedBackView:) name:UIDeviceOrientationDidChangeNotification object:nil];

}
-(void)deviceRotatedFeedBackView:(NSNotification*)notification
{
    //right whetever you want
}
 - (void)viewWillDisappear:(BOOL)animated
{

  [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}

may be it will helpful to you.

Dev Patel
  • 290
  • 1
  • 9
1

Yes it'll remove all the observers in your class.

You can use following to remove single observer:

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"getSipNotification" object:nil];

To remove individual observer.

Rushi
  • 4,553
  • 4
  • 33
  • 46
0

viewDidUnload is deprecated in iOS6 and later, so Your observer never be removed from notification center in iOS6 and later. To remove single observer try

 - (void)removeObserver:(id)notificationObserver name:(NSString *)notificationName object:(id)notificationSender 
HereTrix
  • 1,315
  • 7
  • 13