iOS:UIAlertController 上的 textField 成为出现警报时的第一响应者
iOS: textField on UIAlertController becoming first responder on alert present
我正在使用以下代码将 textField 添加到 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 时,文本字段自动成为第一响应者。我不希望文本字段在显示警报时成为第一响应者。我用谷歌搜索并尝试了几种方法来避免这种情况但没有运气。谁能告诉我如何做到这一点?
您可以暂时禁用UITexField
,它不能在警报出现时响应:
anAlertController.addTextField { (textField) in
textField.textAlignment = NSTextAlignment.center
textField.text = "textFieldText"
textField.isEnabled = false
DispatchQueue.main.async { textField.isEnabled = true }
}
试试这个
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
}
})
我正在使用以下代码将 textField 添加到 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 时,文本字段自动成为第一响应者。我不希望文本字段在显示警报时成为第一响应者。我用谷歌搜索并尝试了几种方法来避免这种情况但没有运气。谁能告诉我如何做到这一点?
您可以暂时禁用UITexField
,它不能在警报出现时响应:
anAlertController.addTextField { (textField) in
textField.textAlignment = NSTextAlignment.center
textField.text = "textFieldText"
textField.isEnabled = false
DispatchQueue.main.async { textField.isEnabled = true }
}
试试这个
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
}
})