iOS将NSNotificationCenter添加到UIView的例外情况

时间:2015-10-12 09:14:42

标签: ios objective-c iphone uiview

我向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];
}

2 个答案:

答案 0 :(得分:2)

您需要删除self作为观察者,而不是您用来处理通知的selector

[[NSNotificationCenter defaultCenter] removeObserver:self];

或者,如果您想要具体,可以使用

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"updateProfile" object:nil];

答案 1 :(得分:1)

始终

在<{1}}

添加 NSNotificationCenter

删除viewDidAppear中的 NSNotificationCenter

相关问题