user2014474
user2014474

Reputation: 1117

Remove NSNotificationCenter observer

I am detecting the showing/hiding of the keyboard by adding this code in the ViewDidLoad:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(keyboardDidHide:) 
                                             name:UIKeyboardDidHideNotification 
                                           object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(keyboardWillShow:) 
                                             name:UIKeyboardWillShowNotification 
                                           object:nil];

At some point though I want to remove these observers, without calling

 [[NSNotificationCenter defaultCenter] removeObserver:self];

because this removes all observers, and I have other observers that I don't want to be removed. How can I remove only those two??

Upvotes: 7

Views: 11332

Answers (3)

Yaman
Yaman

Reputation: 3991

[[NSNotificationCenter defaultCenter] removeObserver:self 
                                                name:UIKeyboardDidHideNotification 
                                              object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self 
                                                name:UIKeyboardWillShowNotification 
                                              object:nil];

Upvotes: 31

MrTJ
MrTJ

Reputation: 13192

Use [[NsNotificationCenter defaultCenter] removeObserver:self name: UIKeyboardWillShowNotification object:nil]

Upvotes: 1

Trebor
Trebor

Reputation: 308

Use the removeObserver:name:object: method of NSNotificationCentre as described in the official documentation, to remove an observer for a particular notification name.

Upvotes: 1

Related Questions