2013-07-24 147 views
1

我知道鍵盤被隱藏後需要執行一些代碼。方法完成後執行代碼

我一直都在以塊,但我只是不理解他們是如何工作的,足以做到這一點...

所有我想要做的就是運行[自hidekeyboard]然後當完成(和鍵盤完全隱藏),然後我想調用一個委託。

什麼是處理這個問題的最好方法?

回答

1

使用NSNotificationCenter類註冊爲UIKeyboardDidHideNotification的監聽器。

[[NSNotificationCenter defaultCenter] 
    addObserver:self 
     selector:@selector(keyboardHidden:) 
      name:UIKeyboardDidHideNorification 
     object:nil]; 

- (void)keyboardHidden:(NSNotification *)notif 
{ 
    // do stuff 
} 

(不要忘記刪除觀察者在- dealloc所以沒有消息會被錯誤地發送到釋放對象。)

+0

您可能還需要在willDidDisAppear可能跟removeObserver:方法。 – Geek

+0

@Aash在'-dealloc'中。 – 2013-07-24 15:25:50

+0

在大多數情況下,這將是解決方案,但我不能使用這些方法,因爲它們用於其他條件。添加到這些方法的委託調用會在其他地方破壞事情,並開始變得混亂。我不得不開始與我的選擇器進行交鋒,我不想走這條路 – JMD

2
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onKeyboardDidHide:) name: UIKeyboardDidHideNotification object:nil]; 

而且onKeyboardDidHide

-(void)onKeyboardDidHide:(NSNotification *)notification 
{ 
    // execute what you want. 
} 
+0

您可能還想在'willDidDisAppear:'方法中調用'removeObserver'。 – Geek