문제

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

도움이 되었습니까?

해결책

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

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

다른 팁

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

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top