Reputation: 19
I added a NSNotificationCenter
to a UIView
, when I first go to the page, the NSNotificationCenter
work fine.
However, when I left that page and back to that page again, it will give out error
'NSInvalidArgumentException', reason: '-[UITextMagnifierTimeWeightedPoint updateProfile:]: unrecognized selector sent to instance.
Here are the code.
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];
}
Upvotes: 0
Views: 293
Reputation: 8511
You need to remove self
as the observer not the selector
you are using to handle the notification
[[NSNotificationCenter defaultCenter] removeObserver:self];
Or if you want to be specific you can use
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"updateProfile" object:nil];
Upvotes: 3
Reputation: 31647
Always
Add NSNotificationCenter
in viewDidAppear
And
Remove NSNotificationCenter
in viewDidDisAppear
Upvotes: 1