【问题标题】:Remove NSNotificationCenter observer移除 NSNotificationCenter 观察者
【发布时间】:2013-02-02 13:23:34
【问题描述】:

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

因为这会移除所有观察者,而我还有其他不想移除的观察者。我怎样才能只删除这两个??

【问题讨论】:

    标签: ios objective-c nsnotificationcenter nsnotifications


    【解决方案1】:
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                    name:UIKeyboardDidHideNotification 
                                                  object:nil];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                    name:UIKeyboardWillShowNotification 
                                                  object:nil];
    

    【讨论】:

      【解决方案2】:

      in the official documentation 所述,使用NSNotificationCentreremoveObserver:name:object: 方法删除特定通知名称的观察者。

      【讨论】:

        【解决方案3】:

        使用[[NsNotificationCenter defaultCenter] removeObserver:self name: UIKeyboardWillShowNotification object:nil]

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-06-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多