Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access input from UIAlertController

I've got a UIAlertController which is prompted to the user when they choose "Enter Password" on the TouchID screen. How do I recover the user's input after presenting this on screen?

let passwordPrompt = UIAlertController(title: "Enter Password", message: "You have selected to enter your passwod.", preferredStyle: UIAlertControllerStyle.Alert)
passwordPrompt.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: nil))
passwordPrompt.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

passwordPrompt.addTextFieldWithConfigurationHandler({(textField: UITextField!) in
                        textField.placeholder = "Password"
                        textField.secureTextEntry = true
                        })

presentViewController(passwordPrompt, animated: true, completion: nil)

I know the OK button should probably have a handler, but right now this code doesn't really do anything, but I want to display the output of the text field through a println. This is really just a test app for me to learn Swift and some new API stuff.

like image 633
Andrew Avatar asked Jun 11 '14 21:06

Andrew


4 Answers

I've written up a blog post exploring the new API. You can just capture a local variable in the closure and you're good to go.

var inputTextField: UITextField?
let passwordPrompt = UIAlertController(title: "Enter Password", message: "You have selected to enter your password.", preferredStyle: UIAlertControllerStyle.Alert)
passwordPrompt.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: nil))
passwordPrompt.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (action) -> Void in
    // Now do whatever you want with inputTextField (remember to unwrap the optional)
}))
passwordPrompt.addTextFieldWithConfigurationHandler({(textField: UITextField!) in
    textField.placeholder = "Password"
    textField.secureTextEntry = true
    inputTextField = textField
 })

presentViewController(passwordPrompt, animated: true, completion: nil)

That way you avoid having an unnecessary property on your view controller.

like image 200
Ash Furrow Avatar answered Nov 15 '22 01:11

Ash Furrow


I know comments have been posted to answer the question, but an answer should make the solution explicit.

@IBOutlet var newWordField: UITextField
func wordEntered(alert: UIAlertAction!){
    // store the new word
    self.textView2.text = deletedString + " " + self.newWordField.text
}
func addTextField(textField: UITextField!){
    // add the text field and make the result global
    textField.placeholder = "Definition"
    self.newWordField = textField
}

// display an alert
let newWordPrompt = UIAlertController(title: "Enter definition", message: "Trainging the machine!", preferredStyle: UIAlertControllerStyle.Alert)
newWordPrompt.addTextFieldWithConfigurationHandler(addTextField)
newWordPrompt.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: nil))
newWordPrompt.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: wordEntered))
presentViewController(newWordPrompt, animated: true, completion: nil)

This displays a prompt with a text field and two action buttons. It reads the text field when done.

like image 32
Scott Avatar answered Nov 15 '22 01:11

Scott


Do this inside your closure:

let tf = alert.textFields[0] as UITextField

Here is a gist

like image 8
Gene De Lisa Avatar answered Nov 15 '22 03:11

Gene De Lisa


I ported Ash Furrow's answer to Swift 3.

    var inputTextField: UITextField?
    let passwordPrompt = UIAlertController(title: "Enter Password", message: "You have selected to enter your password.", preferredStyle: UIAlertControllerStyle.alert)
    passwordPrompt.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default, handler: nil))
    passwordPrompt.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: { (action) -> Void in
        // Now do whatever you want with inputTextField (remember to unwrap the optional)
    }))
    passwordPrompt.addTextField(configurationHandler: {(textField: UITextField!) in
        textField.placeholder = "Password"
        textField.isSecureTextEntry = true
        inputTextField = textField
    })

    present(passwordPrompt, animated: true, completion: nil)
like image 8
Andy Avatar answered Nov 15 '22 01:11

Andy