Henry oscannlain-miller Henry oscannlain-miller - 1 year ago 154
Swift Question

Swift UIAlertController Getting Text Field Text

I need to get the text from the text fields in my alert view when the Input button is pressed.

func inputMsg() {

var points = ""
var outOf = ""

var alertController = UIAlertController(title: "Input View", message: "Please Input Your Grade", preferredStyle: UIAlertControllerStyle.Alert)

let actionCancle = UIAlertAction(title: "Cancle", style: UIAlertActionStyle.Cancel) { ACTION in

let actionInput = UIAlertAction(title: "Input", style: UIAlertActionStyle.Default) { ACTION in


alertController.addTextFieldWithConfigurationHandler({(txtField: UITextField!) in
txtField.placeholder = "I got"
txtField.keyboardType = UIKeyboardType.NumberPad
points = txtField.text

alertController.addTextFieldWithConfigurationHandler({(txtField: UITextField!) in
txtField.placeholder = "Out Of"
txtField.keyboardType = UIKeyboardType.NumberPad
outOf = txtField.text
presentViewController(alertController, animated: true, completion: nil)

Answer Source

The UIAlertController has a textFields property. That's its text fields. Any of your handlers can examine it and thus can get the text from any of the text fields.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download