我想呈现带有文本字段的UIAlertController。这个完成了。但我不希望它成为第一响应者。有没有办法用文本字段呈现UIAlertController,但textfield不应该是第一响应者?
func showAlertData(data: String, at indexPath: IndexPath) {
let title = data
let alert = UIAlertController(title: renameDocumentString, message: renameDocumentMessageString, preferredStyle: UIAlertControllerStyle.alert)
alert.addTextField(configurationHandler: {(textField: UITextField) in
textField.text = title
textField.addTarget(self, action: #selector(self.textChanged(_: )), for: .editingChanged)
textField.addTarget(self, action: #selector(self.textDidBegin(_: )), for: .editingDidBegin)
})
let cancel = UIAlertAction(title: cancelString, style: UIAlertActionStyle.cancel, handler: { (_) -> Void in
alert.dismiss(animated: true, completion: nil)
})
let action = UIAlertAction(title: alertButtonOkString, style: UIAlertActionStyle.default, handler: { (_) -> Void in
if let textfield = alert.textFields?.first {
}
})
alert.addAction(cancel)
alert.addAction(action)
self.actionToEnable = action
action.isEnabled = false
self.present(alert, animated: true, completion: nil)
}
此代码将UIAlertController与textfield一起呈现为响应者。我不想在呈现UIAlertController时成为响应者。
答案 0 :(得分:2)
这不是理想的解决方案,但根据您的要求,在添加AlertController时禁用文本字段,并在呈现AlertController后再次启用。
@IBAction func btnClick(_ sender: UIButton) {
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
alert.addTextField { (textField) in
//Disable textfield
textField.isEnabled = false
}
let okAction = UIAlertAction(title: "Ok", style: .default)
alert.addAction(okAction)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alert, animated: true) {
//Enable textfield
alert.textFields?.first?.isEnabled = true
}
}
答案 1 :(得分:0)
根据您的要求,这是一个完美的解决方案,请参考以下代码
if