我向NSNotificationCenter
添加了UIView
,当我第一次访问该页面时,NSNotificationCenter
正常工作。
但是,当我离开该页面并再次返回该页面时,它会发出错误
'NSInvalidArgumentException', reason: '-[UITextMagnifierTimeWeightedPoint updateProfile:]: unrecognized selector sent to instance.
以下是代码。
UIView1:
- (void)changeUIView {
UIView2 *view = [[UIView2 alloc] init];
// show UIView2
}
UIView2:
- (id)init {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateProfile:) name:@"updateProfile" object:nil];
return self;
}
-(void)updateProfile:(NSNotification *)notification {
// do something
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:@"updateProfile"];
}
- (void)buttonClick {
[[NSNotificationCenter defaultCenter] postNotificationName:@"updateProfile" object:nil userInfo:nil];
}
答案 0 :(得分:2)
您需要删除self
作为观察者,而不是您用来处理通知的selector
[[NSNotificationCenter defaultCenter] removeObserver:self];
或者,如果您想要具体,可以使用
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"updateProfile" object:nil];
答案 1 :(得分:1)
始终
在<{1}} 中 添加 NSNotificationCenter
和
删除viewDidAppear
中的 NSNotificationCenter