Ios 我撞车了。为什么我的班级会收到通知?
在AViewController中,我做下一件事Ios 我撞车了。为什么我的班级会收到通知?,ios,nsnotificationcenter,Ios,Nsnotificationcenter,在AViewController中,我做下一件事 [self.navigationController popViewControllerAnimated:NO]; [[AppDelegate delegate].tabBarController setSelectedIndex:1]; AViewController将被释放,BViewController将出现(这是第一个选项卡) 我撞车了 1) AViewController调用dealloc 2) BViewController发送通知
[self.navigationController popViewControllerAnimated:NO];
[[AppDelegate delegate].tabBarController setSelectedIndex:1];
AViewController将被释放,BViewController将出现(这是第一个选项卡)
我撞车了
1) AViewController调用dealloc
2) BViewController发送通知
3) 我在AViewController的onRotation方法中遇到崩溃
为什么AViewController会收到ntf_onRotation通知?我添加了removeObserver方法
我的课
@implementation AViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onRotation:)
name:@"ntf_onRotation"
object:nil];
}
-(void) viewDidUnload
{
[super viewDidUnload];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ntf_onRotation" object:nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ntf_onRotation" object:nil];
[super dealloc];
}
@end
@implementation BViewController
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[[NSNotificationCenter defaultCenter] postNotificationName:@"ntf_onRotation" object:nil];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] postNotificationName:@"ntf_onRotation" object:nil];
}
@end
这将删除所有观察者,包括任何未知的观察者:
[[NSNotificationCenter defaultCenter] removeObserver:self];
您是否检查了所有其他对AViewController的引用?如果AViewController仍然收到通知,则表示AViewController处于活动状态。我看到有人叫它。这意味着AViewController的保留计数等于零。