ios – 删除NSNotificationCenter观察者

前端之家收集整理的这篇文章主要介绍了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相关文章