Ios NSNotificationCenter线程在第二次调用时中断,即使已删除
我正在为两个不同的ViewController使用Ios NSNotificationCenter线程在第二次调用时中断,即使已删除,ios,objective-c,ios8,nsnotificationcenter,Ios,Objective C,Ios8,Nsnotificationcenter,我正在为两个不同的ViewController使用NSNotificationCenter。第一次调用NSNotificationCenter效果很好,之后我删除了观察者。但是如果我再次运行它,即使移除了观察者,线程也会断开 在ViewController1中: [[NSNotificationCenter defaultCenter] postNotificationName:@"textUpdateNotification" object: nil ]; [[NSNotificatio
NSNotificationCenter
。第一次调用NSNotificationCenter
效果很好,之后我删除了观察者。但是如果我再次运行它,即使移除了观察者,线程也会断开
在ViewController1中:
[[NSNotificationCenter defaultCenter] postNotificationName:@"textUpdateNotification" object: nil ];
[[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];
}
[[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];
}
我试着在-(void)dealloc
中删除观察者,同样的事情也在发生。
尝试在
视图中删除观察者
-(void)viewDidDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"textUpdateNotification" object:nil];
}
对不起,我打错了,
我正在触发NSNotification的源ViewController中删除observer,我应该在使用它的第二个ViewController中删除它
在ViewController1中:
[[NSNotificationCenter defaultCenter] postNotificationName:@"textUpdateNotification" object: nil ];
[[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];
}
[[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];
}
感谢大家发布stacktrace.notificationcenter在que中不起作用,我们需要删除ObserverStackTrace似乎与您的问题无关…实际上我获得了错误的线程访问权限。它没有在控制台中显示任何内容什么是“线程错误访问”?崩溃发生在pickerdissal:
(SOLS\u AS\u CreateAccountDetailViewController
)中。(您应该真正使用Objective-C命名约定。)