iOS版:在UIAlertController文本框变得警惕当前的第一响应者

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

我添加文本框来UIAlertController用下面的代码:

let anAlertController = UIAlertController(title:title, message:message, preferredStyle: .alert)
        weak var weakSelf = self

        let aDefaultAction = UIAlertAction(title:defaultTitle, style: UIAlertAction.Style.default) {
            UIAlertAction in

        anAlertController.addAction(aDefaultAction)
    }

    let aCancelAction = UIAlertAction(title:canceltTitle, style: UIAlertAction.Style.cancel) {
        UIAlertAction in
    }
    anAlertController.addAction(aCancelAction)

    anAlertController.addTextField { (textField) in
        textField.textAlignment = NSTextAlignment.center
        textField.text = "textFieldText"
        textField.delegate = self
        textField.resignFirstResponder()
    }
    self.present(anAlertController, animated: true, completion: nil)

当我尝试提出UIAlertController,文本框会自动成为第一个响应者。我不想文本框应该成为第一个响应者,同时提出警告。我一派,并试图多种方法来避免这种情况,但没有运气。谁能告诉如何实现这一目标?

ios swift uialertcontroller first-responder
2个回答
1
投票

尝试这个

anAlertController.addTextField { (textField) in
        textField.textAlignment = .center
        textField.text = "textFieldText"
        textField.delegate = self as? UITextFieldDelegate
        textField.isEnabled = false

    }
    self.present(anAlertController, animated: false, completion: {
        if let getTextfield  = anAlertController.textFields?.first{
                getTextfield.resignFirstResponder()
                getTextfield.isEnabled = true
        }

    })

0
投票

您可以禁用UITexField了一会儿,就不能对警报响应出现:

anAlertController.addTextField { (textField) in
    textField.textAlignment = NSTextAlignment.center
    textField.text = "textFieldText"
    textField.isEnabled = false
    DispatchQueue.main.async { textField.isEnabled = true }
}
© www.soinside.com 2019 - 2024. All rights reserved.