NSNotificationCenter 线程在第二次调用时中断,即使它已被删除
NSNotificationCenter Thread breaks at second call even after it has been removed
我正在为两个不同的 ViewController 使用 NSNotificationCenter
。第一次调用 NSNotificationCenter
效果很好,之后我删除了观察者。但是如果我再次 运行 它,线程在移除观察者后甚至会中断。
在 ViewController1 中:
[[NSNotificationCenter defaultCenter] postNotificationName:@"textUpdateNotification" object: nil ];
在 ViewController2 中:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:@"textUpdateNotification" object:nil];
-(void)receiveNotification:(NSNotification *)notificaton
{
....
....
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"textUpdateNotification" object:nil];
}
我尝试移除 - (void)dealloc
中的观察者,那里也发生了同样的事情。
尝试移除 viewDidDisappear
中的观察者
-(void)viewDidDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"textUpdateNotification" object:nil];
}
对不起,我的错字,
我正在从触发 NSNotification 的源 ViewController 中删除观察者,我应该从使用它的第二个 ViewController 中删除它。
在 ViewController1:
[[NSNotificationCenter defaultCenter] postNotificationName:@"textUpdateNotification" object: nil ];
在 ViewController2:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:@"textUpdateNotification" object:nil];
-(void)receiveNotification:(NSNotification *)notificaton
{
....
....
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"textUpdateNotification" object:nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"textUpdateNotification" object:nil];
}
谢谢大家
我正在为两个不同的 ViewController 使用 NSNotificationCenter
。第一次调用 NSNotificationCenter
效果很好,之后我删除了观察者。但是如果我再次 运行 它,线程在移除观察者后甚至会中断。
在 ViewController1 中:
[[NSNotificationCenter defaultCenter] postNotificationName:@"textUpdateNotification" object: nil ];
在 ViewController2 中:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:@"textUpdateNotification" object:nil];
-(void)receiveNotification:(NSNotification *)notificaton
{
....
....
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"textUpdateNotification" object:nil];
}
我尝试移除 - (void)dealloc
中的观察者,那里也发生了同样的事情。
尝试移除 viewDidDisappear
-(void)viewDidDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"textUpdateNotification" object:nil];
}
对不起,我的错字, 我正在从触发 NSNotification 的源 ViewController 中删除观察者,我应该从使用它的第二个 ViewController 中删除它。
在 ViewController1:
[[NSNotificationCenter defaultCenter] postNotificationName:@"textUpdateNotification" object: nil ];
在 ViewController2:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:@"textUpdateNotification" object:nil];
-(void)receiveNotification:(NSNotification *)notificaton
{
....
....
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"textUpdateNotification" object:nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"textUpdateNotification" object:nil];
}
谢谢大家