UIButton没有得到关注

问题描述 投票:0回答:1

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

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

谢谢。

swift3 uibutton uitextfield uitextfielddelegate
1个回答
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
}
© www.soinside.com 2019 - 2024. All rights reserved.