2013-02-02 151 views
7

我加入這個代碼在viewDidLoad中檢測鍵盤的顯示/隱藏:刪除NSNotificationCenter觀察者

[[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]; 

,因爲這會刪除所有觀察者,並且我有其他觀察員不希望被刪除。我怎樣才能刪除這兩個?

回答

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

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

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