ios – 删除NSNotificationCenter观察者

前端之家收集整理的这篇文章主要介绍了ios – 删除NSNotificationCenter观察者前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我通过在ViewDidLoad中添加代码来检测键盘显示/隐藏:
  1. [[NSNotificationCenter defaultCenter] addObserver:self
  2. selector:@selector(keyboardDidHide:)
  3. name:UIKeyboardDidHideNotification
  4. object:nil];
  5.  
  6. [[NSNotificationCenter defaultCenter] addObserver:self
  7. selector:@selector(keyboardWillShow:)
  8. name:UIKeyboardWillShowNotification
  9. object:nil];

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

  1. [[NSNotificationCenter defaultCenter] removeObserver:self];

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

解决方法

  1. [[NSNotificationCenter defaultCenter] removeObserver:self
  2. name:UIKeyboardDidHideNotification
  3. object:nil];
  4.  
  5. [[NSNotificationCenter defaultCenter] removeObserver:self
  6. name:UIKeyboardWillShowNotification
  7. object:nil];

猜你在找的iOS相关文章