ios – 删除NSNotificationCenter观察者

我通过在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];

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

解决方法

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

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

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...