Remove NSNotificationCenter observer
Asked Answered
R

3

7

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??

Remittee answered 2/2, 2013 at 13:23 Comment(0)
E
31
[[NSNotificationCenter defaultCenter] removeObserver:self 
                                                name:UIKeyboardDidHideNotification 
                                              object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self 
                                                name:UIKeyboardWillShowNotification 
                                              object:nil];
Ellingson answered 2/2, 2013 at 13:25 Comment(0)
W
1

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

Whist answered 2/2, 2013 at 13:31 Comment(0)
R
1

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

Ravel answered 2/2, 2013 at 13:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.