This question already has answers here:
Access input from UIAlertController

(4 个回答)


5 天前关闭。




我正在尝试从 UIAlertController 获取 textField 值,但是每当我尝试输入该值并尝试将其打印出来时,输出都不会显示任何内容。

代码:
var alert = UIAlertController(title: "Enter the password", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
alert.addTextFieldWithConfigurationHandler({(textField: UITextField!) in
                textField.placeholder = "Enter text:"
                textField.secureTextEntry = true
                println(textField.text)

      })
self.presentViewController(alert, animated: true, completion: nil

最佳答案

试试这个代码:

var inputTextField: UITextField?
let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)
let ok = UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in
    // Do whatever you want with inputTextField?.text
    println("\(inputTextField?.text)")
})

let cancel = UIAlertAction(title: "Cancel", style: .Cancel) { (action) -> Void in
}
alertController.addAction(ok)
alertController.addAction(cancel)
alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in
    inputTextField = textField
}
presentViewController(alertController, animated: true, completion: nil)

关于ios - 如何在 UIAlertController 中获取 textField 的值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30344906/

10-14 21:41