如何禁用UIAlertController按动作按钮时自动关闭
问题描述:
我有一个UIAlertController
,我正在检查用户输入。当用户没有输入文本字段时,我添加的OK Action按钮应该给用户一个警告并且不关闭警报视图。如何禁用UIAlertController按动作按钮时自动关闭
我处理警告,但警报视图自动关闭。 我如何禁用自动关闭?
谢谢。
我的代码:
var alert = UIAlertController(title: "change name and phone number", message: nil, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default,
handler: { action in
//Add a comment to this line
let nameField: UITextField = alert.textFields![0] as UITextField
let phoneField: UITextField = alert.textFields![1] as UITextField
let name = nameField.text
let phone = phoneField.text
if name.length == 0 {
JLToast.makeText("Please enter name").show()
} else if phone.length == 0 {
JLToast.makeText("Please enter phone number").show()
} else {
self.sendSupportInfo(nameField.text, phone: phoneField.text)
}
println("name:: \(nameField.text), phone: \(phoneField.text)")
}))
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: nil))
alert.addTextFieldWithConfigurationHandler { (textField) -> Void in
textField.placeholder = "name"
}
alert.addTextFieldWithConfigurationHandler { (textField) -> Void in
textField.placeholder = "0544-444444"
textField.keyboardType = UIKeyboardType.PhonePad
}
self.presentViewController(alert, animated: true, completion: nil)
答
你不能做到这一点。 其他选项是,您只能禁用UIAlertAction。相反,您可能想要为此创建自己的自定义对话框。
的官方文件说:
子类 的UIAlertController类是打算按原样使用,不支持子类。该类的视图层次结构是私有的,不能修改。
答
U可以创建一个新的UI窗口,并使窗口级别高于警戒级别,然后在新的UI窗口上显示警报。
[阻止UIAlertController关闭]的可能重复(http://stackoverflow.com/questions/28919670/prevent-uialertcontroller-to-dismiss) – soulshined 2015-04-02 20:52:59