How to add TextField to UIAlertController in Swift
Solution 1
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)
}
Solution 2
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 3
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.
Solution 4
So I started checking to see what could possibly have been different in my code to the working code. I noticed that my ViewController extends
UITextFieldDelegate
Which apparently means that I need to set the delegate of any child UITextView:
alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in
searchTextField = textField
searchTextField?.delegate = self //REQUIRED
searchTextField?.placeholder = "Enter your search terms"
}
Solution 5
How to add textField to AlertView? Let's keep it short and simple.
This works for Swift 3.0 and above.
var nameField: UITextField?
let alertController = UIAlertController(title: "Add Number", message: nil, preferredStyle: .alert)
// Add textfield to alert view
alertController.addTextField { (textField) in
nameField = textField
}
First, you instantiate an object of UIAlertController
and then you add a text field to it by accessing addTextField
member of UIAlertController
class.
Related videos on Youtube
Comments
-
Quintin Balsdon over 2 years
I am trying to show a
UIAlertController
with aUITextView
. When I add the line://Add text field alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in }
I get a
Runtime
error:fatal error: unexpectedly found nil while unwrapping an Optional value
let alertController: UIAlertController = UIAlertController(title: "Find image", message: "Search for image", preferredStyle: .Alert) //cancel button let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in //cancel code } alertController.addAction(cancelAction) //Create an optional action let nextAction: UIAlertAction = UIAlertAction(title: "Search", style: .Default) { action -> Void in let text = (alertController.textFields?.first as! UITextField).text println("You entered \(text)") } alertController.addAction(nextAction) //Add text field alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in textField.textColor = UIColor.greenColor() } //Present the AlertController presentViewController(alertController, animated: true, completion: nil)
This is presented inside my
ViewController
via anIBAction
.I have downloaded the code from here and it works fine. I copied and pasted that method into my code and it breaks. The presence of self on the last line has no impact.
-
benc almost 5 years
UITextField
, rather thanUITextView
?
-
-
benc almost 5 yearsIs the order of the two
alertController.addTextField
correct? It seems like "Second Name" would appear above "First Name". -
Phontaine Judd about 3 yearsThis is what I was looking for. I wanted to restrict characters within the text field inside the alert controller. I like your idea of declaring the
textField
outside the controller. That way I can usetextField
shouldChangeCharactersInRange
. Really good idea. Thanks!