删除NSNotificationCenter观察者

use*_*474 7 objective-c nsnotifications nsnotificationcenter ios

我通过在ViewDidLoad中添加此代码来检测键盘的显示/隐藏:

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

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(keyboardWillShow:) 
                                             name:UIKeyboardWillShowNotification 
                                           object:nil];
Run Code Online (Sandbox Code Playgroud)

在某些时候,我想删除这些观察者,而不是打电话

 [[NSNotificationCenter defaultCenter] removeObserver:self];
Run Code Online (Sandbox Code Playgroud)

因为这会删除所有观察者,我还有其他观察者,我不想删除它们.我怎么才能删除那两个?

Yam*_*man 31

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

[[NSNotificationCenter defaultCenter] removeObserver:self 
                                                name:UIKeyboardWillShowNotification 
                                              object:nil];
Run Code Online (Sandbox Code Playgroud)