我如何在 UIAlertController 中获取 textField 的值?

How do i get a value of textField in an UIAlertController?

我正在尝试从 UIAlertController 获取文本字段值,但每当我尝试输入该值并尝试将其打印出来时,输出都没有显示任何内容。

代码:

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)