簡體   English   中英

為什么 becomeFirstResponder 不會在警報消息文本字段上觸發軟鍵盤

[英]Why doesn't becomeFirstResponder trigger soft keyboard on alert message textField

我有一個 UIAlertController,我的應用程序上顯示了一個textField textField被選中(標記在其中閃爍)並且.becomeFirstResponder被設置在textField上。 但由於某種原因, soft-keyboard沒有顯示。 我打印了 boolean isFirstResponder並返回false

我在某處讀到它必須在viewDidLoad中觸發,但在這種情況下這是不可能的,因為通過按下viewDidLoad之外的function中的button來顯示警報

這是我的代碼:

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)。

我知道這一點很長時間了,但有時我仍然會感到困惑,為什么直到我記得檢查鍵盤才會出現(主要是在使用模擬器時)。 很容易錯過,所以請確保這里不是這種情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM