2017-10-18 190 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 
}