我通过在ViewDidLoad中添加此代码来检测键盘的显示/隐藏:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
虽然我想在不调用
的情况下删除这些观察者 [[NSNotificationCenter defaultCenter] removeObserver:self];
因为这会删除所有观察者,而我还有其他观察者不希望被删除。我怎样才能删除那两个??
答案 0 :(得分:31)
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardDidHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
答案 1 :(得分:1)
使用removeObserver:name:object:
所述NSNotificationCentre
{{1}}方法,删除特定通知名称的观察者。
答案 2 :(得分:1)
使用[[NsNotificationCenter defaultCenter] removeObserver:self name: UIKeyboardWillShowNotification object:nil]