我有一个UIAlertController,在我的应用程序上显示textField。选择textField(标记在其中闪烁)并在.becomeFirstResponder上设置textField。但是由于某种原因,soft-keyboard没有显示出来。我打印了一个布尔值isFirstResponder,它返回了false
我在某个地方看到它必须在viewDidLoad中触发,但在这种情况下这是不可能的,因为警报是通过从button之外的function按aviewDidLoad来显示的
这是我的代码:

func verificationPopup(title: String, message: String, codeShouldBeVerified: Bool, context: UIViewController, callback: @escaping() -> Void) {
    let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)

    //THIS IS THE RELEVANT PART
    if(codeShouldBeVerified) {
        alert.addTextField(configurationHandler: { textField in
            textField.placeholder = "Fyra siffror"
            textField.textAlignment = NSTextAlignment.center
            textField.delegate = self as? UITextFieldDelegate
            textField.becomeFirstResponder()
            print("HERE", textField.isFirstResponder)
        })
        alert.addAction(UIAlertAction(title: "Jag fick ingen kod", style: .default, handler: { action in
            callback()
        }))

    }
    else {
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
            callback()
            //self.verifyNumber()
        }))

        alert.addAction(UIAlertAction(title: "Avbryt", style: .cancel, handler: {
            action in
            print("CAAAANCEL")
        }))
    }
    context.present(alert, animated: true)
}

当我刚设置时.isFirstResponder返回false似乎很奇怪。怎么回事?

最佳答案

在某些情况下,软键盘不会出现是正常的。
(a)当蓝牙键盘连接到实际设备时,或
(b)在设备模拟器中模拟硬件键盘时(菜单:hardware->keyboard->Connect hardware keyboard)。
我知道这一点很长一段时间了,但有时我还是会感到困惑,为什么键盘不会出现,直到我记得检查(主要是在使用模拟器时)。很容易漏掉,所以要确保这里不是这样。

10-08 05:47
查看更多