Search code examples
iosobjective-cexc-bad-accessnsnotificationcenter

NSNotificationCenter crash after changing UIViewController


I'm now implementing an application, whith iOS SDK 6.0, and i've got a problem.

When i'm opening for the first time my UIViewcontroller where i implemented some NSNotificationCenter to detect when the user is clicking on the home button, it looks like working when i try to click on the home button and go back to my app'. But the problem is, when i'm on my UIViewcontroller, and i go to another, get back to this UIViewcontroller and try to click on the home button, the app' looks like crashing.

This is my code :

In the viewDidLoad function :

[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(ApplicationEnteringBackground)
                                             name: UIApplicationDidEnterBackgroundNotification
                                           object: nil];
[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(ApplicationEnteringForeground)
                                             name: UIApplicationWillEnterForegroundNotification
                                           object: nil];

And i just implemented to methods :

-(void)ApplicationEnteringBackground
{
   ...
}
-(void)ApplicationEnteringForeground
{
   ...
}

The problem looks like this :

The problem

Thanks for your help =)


Solution

  • Make sure that you are removing observer when your first view controller is destroyed [[NSNotificationCenter defaultCenter] removeObserver:self];