UIButton没有得到关注

时间:2017-10-18 16:08:14

标签: swift3 uibutton uitextfield uitextfielddelegate

我的代码在Swift 3中。我在视图中有两个UITextField和一个UIButton。我有第一个UITextField的textFieldShouldEndEditing委托,当我专注于第二个UITextField时会触发它。但是当我点击UIButton时,这个代表不会被解雇。我原以为当UIButton获得焦点时,UITextField的事件将自动触发,因为它失去了焦点。但是,当我点击UIButton时,光标仍然在UITextField中闪烁,这意味着它不会失去焦点。

任何关于为什么UIButton没有得到关注的想法都将受到赞赏。

感谢。

1 个答案:

答案 0 :(得分: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
}
相关问题