UIButton没有得到焦点



我在 Swift 3 中有我的代码。在我看来,我有两个UITextFields和一个UIButton。我有第一个UITextField的textFieldShouldEndEdit委托,当我专注于第二个UITextField时,它会触发。但是,当我点击UIButton时,该委托不会被触发。我曾认为当UIButton获得焦点时,UITextField的事件将自动触发,因为它正在失去焦点。但是,当我点击UIButton时,光标仍然在UITextField中闪烁,这意味着它没有失去焦点。

任何关于为什么UIButton没有得到关注的想法将不胜感激。

谢谢。

这是在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
}

最新更新