How to add TextField to UIAlertController in Swift How to add TextField to UIAlertController in Swift ios ios

How to add TextField to UIAlertController in Swift


Swift 5.1

alert.addTextField { (textField) in    textField.placeholder = "Enter First Name"}

Use this code, I am running this code in my app successfully.

@IBAction func addButtonClicked(sender : AnyObject){    let alertController = UIAlertController(title: "Add New Name", message: "", preferredStyle: UIAlertControllerStyle.Alert)    alertController.addTextFieldWithConfigurationHandler { (textField : UITextField!) -> Void in        textField.placeholder = "Enter Second Name"    }    let saveAction = UIAlertAction(title: "Save", style: UIAlertActionStyle.Default, handler: { alert -> Void in        let firstTextField = alertController.textFields![0] as UITextField        let secondTextField = alertController.textFields![1] as UITextField    })    let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: {        (action : UIAlertAction!) -> Void in })    alertController.addTextFieldWithConfigurationHandler { (textField : UITextField!) -> Void in        textField.placeholder = "Enter First Name"    }        alertController.addAction(saveAction)    alertController.addAction(cancelAction)        self.presentViewController(alertController, animated: true, completion: nil)}

Edited: Swift 3.0 version

@IBAction func addButtonClicked(_ sender: UIButton){    let alertController = UIAlertController(title: "Add New Name", message: "", preferredStyle: .alert)    alertController.addTextField { (textField : UITextField!) -> Void in        textField.placeholder = "Enter Second Name"    }    let saveAction = UIAlertAction(title: "Save", style: .default, handler: { alert -> Void in        let firstTextField = alertController.textFields![0] as UITextField        let secondTextField = alertController.textFields![1] as UITextField        print("firstName \(firstTextField.text), secondName \(secondTextField.text)")    })    let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: { (action : UIAlertAction!) -> Void in })    alertController.addTextField { (textField : UITextField!) -> Void in        textField.placeholder = "Enter First Name"    }    alertController.addAction(saveAction)    alertController.addAction(cancelAction)        self.present(alertController, animated: true, completion: nil)}


To add a text field in Swift 3.0:

let alertController = UIAlertController(title: "Title", message: "", preferredStyle: .alert)alertController.addAction(UIAlertAction(title: "Save", style: .default, handler: { alert -> Void in    let textField = alertController.textFields![0] as UITextField    // do something with textField}))alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))alertController.addTextField(configurationHandler: {(textField : UITextField!) -> Void in    textField.placeholder = "Search"})   self.present(alertController, animated: true, completion: nil)


Solution:

Swift 4.2

Try the following lines and see if it works:

let alertController = UIAlertController(title: "Add New Name", message: "", preferredStyle: .alert)alertController.addTextField { (textField : UITextField!) -> Void in    textField.placeholder = "Enter Second Name"}let saveAction = UIAlertAction(title: "Save", style: .default, handler: { alert -> Void in    let firstTextField = alertController.textFields![0] as UITextField    let secondTextField = alertController.textFields![1] as UITextField})let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil )alertController.addTextField { (textField : UITextField!) -> Void in    textField.placeholder = "Enter First Name"}alertController.addAction(saveAction)alertController.addAction(cancelAction)self.present(alertController, animated: true, completion: nil)

Hope it helps.