2017-10-18 188 views
0

我有我的代碼在Swift 3中。我有兩個UITextFields和一個UIButton在我看來。當我關注第二個UITextField時,我有第一個UITextField的textFieldShouldEndEditing委託被觸發。但是,當我點擊UIButton時,這個委託沒有被解僱。我曾認爲當UIButton獲得焦點時,UITextField的事件會自動啓動,因爲它失去焦點。但是,當我點擊UIButton時,遊標仍然在UITextField中閃爍,這意味着它不會失去焦點。UIButton沒有得到重點

爲什麼UIButton沒有得到重點的任何想法將不勝感激。

謝謝。

回答

0

這是在iOS上開發表單的真氣。按鈕水龍頭不會像在HTML表單中那樣從UITextField/UITextView移除焦點。

我確定有更優雅的方法可以解決這個問題,但是如果我想要在所有表單中一致地執行此操作,我確保在用戶點擊我的任何按鈕時運行以下代碼行。

- (void)userDidTapButton:(id)sender 
{ 
    [[[UITextField new] becomeFirstResponder] resignFirstResponder] 
    // the above embarrassing line of code basically creates a new textfield 
    // puts focus in it (thereby removing focus from any existing textfields 
    // and then removes focus again 
    // this ensures that delegate events fire on your existing textfields 

// finally, run any other button code 
}