在AViewController中我做下一件事。
[self.navigationController popViewControllerAnimated:NO];
[[AppDelegate delegate].tabBarController setSelectedIndex:1];
AViewController将被释放,BViewController将出现(它是第一个标签页)。
我崩溃了。
1)AViewController calles 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
答案 0 :(得分:0)
这应该删除所有观察者,包括任何未知的观察者:
[[NSNotificationCenter defaultCenter] removeObserver:self];