When should I add and remove observers from UIApplication notifications?
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; [nc addObserver:self selector:@selector(saveState) name:UIApplicationWillResignActiveNotification object:nil]; [nc addObserver:self selector:@selector(loadState) name:UIApplicationWillEnterForegroundNotification object:nil]; }
and
- (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self.navigationController setNavigationBarHidden:YES animated:animated]; NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; [nc removeObserver:self name:UIApplicationWillResignActiveNotification object:nil]; [nc removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil]; }
This is bad? I'm only interested in notifications when a view is displayed on the screen. And will there be problems removing UIApplicationWillEnterForegroundNotification in the viewWillDisappear: method? I think of the order in which everything happens ...?
source share