我应该何时添加和删除 UIApplication 通知的观察者?
- (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];
}
和
- (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];
}
这很糟糕吗?当视图在屏幕上时,我只对通知感兴趣。UIApplicationWillEnterForegroundNotification
删除方法中的会有什么问题viewWillDisappear:
吗?我在想事情发生的顺序……?