Checking the value in textfield before alert controller hides - iOS swift - ios

What I am trying to do is that I am taking user's contact number (mobile number) if it is correct then it should continue the signup otherwise the alert controller should not hide and show some error
let alertController = UIAlertController(title: "Phone Number", message: "Please enter your number", preferredStyle: .alert)
alertController.addTextField {
(textField) -> Void in
textField.tag = 128
textField.delegate = self
textField.placeholder = "923xxxxxxxxx"
}
alertController.addAction(UIAlertAction(title: "Continue", style: .default, handler: {
alert -> Void in
let textField = alertController.textFields![0] as UITextField
if (textField.text?.count)! == 12 && ((textField.text?.substring(to: 3))!) == "923" {
//my code
}
else {
//should show/keep alert controler
}
}))
self.present(alertController, animated: true, completion: nil)

Try out this,
let alertController = UIAlertController(title: "Phone Number", message: "Please enter your number", preferredStyle: .alert)
alertController.addTextField {
(textField) -> Void in
textField.tag = 128
textField.delegate = self
textField.placeholder = "923xxxxxxxxx"
}
alertController.addAction(UIAlertAction(title: "Continue", style: .default, handler: {
alert -> Void in
let textField = alertController.textFields![0] as UITextField
if (textField.text?.count)! == 12 && ((textField.text?.substring(to: 3))!) == "923"
{
//my code
}
else
{
//alert with error
let alertControllerError = UIAlertController(title: "error", message: "error here", preferredStyle: .alert)
alertControllerError.addAction(UIAlertAction(title: "ok", style: .default, handler: { alert -> Void in
}))
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .seconds(1)) {
self.present(alertControllerError, animated: true, completion: nil)
}
}
}))
self.present(alertController, animated: true, completion: nil)

You could disable the button (UIAlertAction) by default. Then listen to text changes in your UITextField and update the button accordingly:
var autoEnableAlertAction: UIAlertAction?
func presentAlertController() {
// alert controller
let ac = UIAlertController(title: "Registration", message: "Please enter your phone number.", preferredStyle: .Alert)
// alert action - disabled by default
let okAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
okAction.enabled = false
ac.addAction(okAction)
// store a reference to the alert action to enable / disable it later
autoEnableAlertAction = okAction
ac.addTextFieldWithConfigurationHandler { tf in
// listen for changes in the textfield
tf.addTarget(self, action: #selector(self.checkTextFieldText(_:)), forControlEvents: .EditingChanged)
}
presentViewController(ac, animated: true, completion: nil)
}
#objc func checkTextFieldText(textField: UITextField) {
autoEnableAlertAction?.enabled = textField.text?.characters.count >= 5
}
Sorry for my answer being a Swift 2 version... :)

func presentAlertController()
{
let alertController = UIAlertController(title: "Phone Number", message: "Please enter your number", preferredStyle: .alert)
alertController.addTextField {
(textField) -> Void in
textField.tag = 128
textField.placeholder = "923xxxxxxxxx"
}
alertController.addAction(UIAlertAction(title: "Continue", style: .default, handler: {
alert -> Void in
let textField = alertController.textFields![0] as UITextField
if (textField.text?.count)! == 12 && textField.text?.prefix(3) == "923"
{
print("sucess")
}
else
{
print("Incorrect Number")
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .seconds(2)) {
self.presentAlertController(parameters: parameters)
}
}
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler:{
alert -> Void in
}))
self.present(alertController, animated: true, completion: nil)
}

Related

UIAlertController textfield not getting input

I am trying to code, an UIAlertController that gets input from the user through a UITextfield.
When i run the print statement nothing gets printed after i type something in the textfield and hit the save button.
#IBAction func reqTimeButton(_ sender: Any) {
// time is requested by student
timer.invalidate()
let alert = UIAlertController(title: "Time Request", message: "Please enter your reason to extend the time of your trip", preferredStyle: .alert)
alert.addTextField { (reason) in
reason.placeholder = "" }
alert.addAction(UIAlertAction(title: "Continue", style: .default, handler: { (action) in
let textField = alert.textFields![0]
textField.text = self.request
self.totalTime = self.totalTime + 480
self.initialTime = self.initialTime + 480
self.runTimer()
print(textField.text!) }))
alert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (action) in
self.runTimer() }))
self.present(alert, animated: true, completion: nil)
}
Use this code to get the text from UITextField, I hope this helps you.
let alertController = UIAlertController(title: "Time Request", message: "Please enter your reason to extend the time of your trip", preferredStyle: .alert)
alertController.addTextField { (textField : UITextField!) -> Void in
textField.placeholder = "Enter your reason"
}
alertController.addAction(UIAlertAction(title: "Continue", style: .default, handler: { (okAction) in
let textField = alertController.textFields![0] as UITextField
print(textField)
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alertController, animated: true, completion: nil)
i figured out what was wrong. This is the working code.
#IBAction func reqTimeButton(_ sender: Any) {
// time is requested by student
timer.invalidate()
let alert = UIAlertController(title: "Time Request", message: "Please enter your reason to extend the time of your trip", preferredStyle: .alert)
alert.addTextField { (reason) in
reason.placeholder = "" }
alert.addAction(UIAlertAction(title: "Continue", style: .default, handler: { (action) in
let textField = alert.textFields![0]
print(textField.text!)
self.totalTime = self.totalTime + 480
self.initialTime = self.initialTime + 480
self.runTimer()
self.request = textField.text!
print(self.request) }))
alert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (action) in
self.runTimer() }))
self.present(alert, animated: true, completion: nil)
}

How to show alert after keyboard has been shown in UITextView textViewDidBeginEditing

Inside textViewDidBeginEditing I'm showing alert using UIAlertController. Alert is shown prior to the keyboard (on simulator).
How do I show keyboard before alert pops up?
func textViewDidBeginEditing(_ textView: UITextView) {
if self.balance <= 0 {
let alert = UIAlertController(title: "Balance Low", message: "Your balance is low.", preferredStyle: UIAlertControllerStyle.alert)
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) { (cancel) in
}
let okAction = UIAlertAction(title: "Buy", style: UIAlertActionStyle.default) { (action) in
self.segueToBuy()
}
alert.addAction(cancelAction)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
}
}
Please use DispatchQueue.main.asyncAfter to show the alert after certain delay, whenever user typed the text in UITextView.
func asyncAfter(deadline: DispatchTime, qos: DispatchQoS = default, flags: DispatchWorkItemFlags = default, execute work: #escaping #convention(block) () -> Void)
Delcare private instance variable which is used to show alert, locally.
var showAlert = true
Try the code shown below in textViewDidBeginEditing:
func textViewDidBeginEditing(_ textView: UITextView) {
if self.showAlert && self.balance <= 0 {
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
textView.endEditing(true)
let alert = UIAlertController(title: "Balance Low", message: "Your balance is low.", preferredStyle: UIAlertController.Style.alert)
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel) { (cancel) in
self.showAlert = false
textView.becomeFirstResponder()
}
let okAction = UIAlertAction(title: "Buy", style: UIAlertAction.Style.default) { (action) in
textView.endEditing(true)
self.segueToBuy()
}
alert.addAction(cancelAction)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
}
}
}

How to add TextField to UIAlertController in Swift

I am trying to show a UIAlertController with a UITextView. 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 an IBAction.
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.
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.
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"
}
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.
UIAlertController with UITextfield in latest Apple Swift version 5.1.3
Create a lazy variable of UIAlertController in your UIViewController , Add UITextFieldDelegate , Show Alert on UIButton Action :
class YourViewController: UIViewController,UITextFieldDelegate {
//Create Alert Controller Object here
lazy var alertEmailAddEditView:UIAlertController = {
let alert = UIAlertController(title:"My App", message: "Customize Add/Edit Email Pop Up", preferredStyle:UIAlertController.Style.alert)
//ADD TEXT FIELD (YOU CAN ADD MULTIPLE TEXTFILED AS WELL)
alert.addTextField { (textField : UITextField!) in
textField.placeholder = "Enter emails"
textField.delegate = self
}
//SAVE BUTTON
let save = UIAlertAction(title: "Save", style: UIAlertAction.Style.default, handler: { saveAction -> Void in
let textField = alert.textFields![0] as UITextField
print("value entered by user in our textfield is: \(textField.text)")
})
//CANCEL BUTTON
let cancel = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.default, handler: {
(action : UIAlertAction!) -> Void in })
alert.addAction(save)
alert.addAction(cancel)
return alert
}()
//MARK:- UIButton Action for showing Alert Controller
#objc func buttonClicked(btn:UIButton){
self.present(alertEmailAddEditView, animated: true, completion: nil)
}
//MARK:- UITextFieldDelegate
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}
Happy Coding!! :)
To add alertController with one textField (Swift 5)
func openAlert(){
let alertController = UIAlertController(title: "Title", message: "", preferredStyle: .alert)
alertController.addTextField { (textField : UITextField!) -> Void in
textField.placeholder = "Enter name"
}
let saveAction = UIAlertAction(title: kAlertConfirm, style: .default, handler: { alert -> Void in
if let textField = alertController.textFields?[0] {
if textField.text!.count > 0 {
print("Text :: \(textField.text ?? "")")
}
}
})
let cancelAction = UIAlertAction(title: kAlertCancel, style: .default, handler: {
(action : UIAlertAction!) -> Void in })
alertController.addAction(cancelAction)
alertController.addAction(saveAction)
alertController.preferredAction = saveAction
self.present(alertController, animated: true, completion: nil)
}
For Swift 4.0, You can use this sample of code succesfully tested in my project:
#IBAction func withdrawTapped(_ sender: UIButton) {
let alertController = UIAlertController(title: "Token withdraw", message: "", preferredStyle: .alert)
let withdrawAction = UIAlertAction(title: "Withdraw", style: .default) { (aciton) in
let text = alertController.textFields!.first!.text!
if !text.isEmpty {
self.presentAlert(
title: "Succesful",
message: "You made request for withdraw \(textField.text) tokens")
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
}
alertController.addTextField { (textField) in
textField.placeholder = "999"
textField.keyboardType = .decimalPad
}
alertController.addAction(withdrawAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
}
In Swift 4.2 and Xcode 10.1
Alert with two Textfields and Read TextField text data and present alert on top of all views.
func alertWithTF(title: String, message: String) {
//Step : 1
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
//Step : 2
alert.addAction (UIAlertAction(title: "Save", style: .default) { (alertAction) in
let textField = alert.textFields![0]
let textField2 = alert.textFields![1]
if textField.text != "" {
//Read textfield data
print(textField.text!)
print("TF 1 : \(textField.text!)")
} else {
print("TF 1 is Empty...")
}
if textField2.text != "" {
//Read textfield data
print(textField2.text!)
print("TF 2 : \(textField2.text!)")
} else {
print("TF 2 is Empty...")
}
})
//Step : 3
//For first TF
alert.addTextField { (textField) in
textField.placeholder = "Enter your first name"
textField.textColor = .red
}
//For second TF
alert.addTextField { (textField) in
textField.placeholder = "Enter your last name"
textField.textColor = .blue
}
//Cancel action
alert.addAction(UIAlertAction(title: "Cancel", style: .default) { (alertAction) in })
self.present(alert, animated:true, completion: nil)
}
If you want to present aleert on top of all views.
Here from above code remove this last line self.present(alert, animated:true, completion: nil) and add below code.
//Present alert on top of all views.
let alertWindow = UIWindow(frame: UIScreen.main.bounds)
alertWindow.rootViewController = UIViewController()
alertWindow.windowLevel = UIWindowLevelAlert + 1
alertWindow.makeKeyAndVisible()
alertWindow.rootViewController?.present(alert, animated:true, completion: nil)
Now call like this
alertWithTF(title: "This is title", message: "This is message")
Great answer a slight modification to show how the textfield can be used:
func addRow (row: Int, bodin: String, flag: Int) {
let alertController = UIAlertController(title: bodin, message: "", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Save", style: .default, handler: {
alert -> Void in
_ = alertController.textFields![0] as UITextField
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alertController.addTextField(configurationHandler: {(textField : UITextField!) -> Void in
switch flag {
case 0:
textField.keyboardType = UIKeyboardType.phonePad
textField.placeholder = "Enter Number"
case 1:
textField.keyboardType = UIKeyboardType.emailAddress
textField.placeholder = "Enter Email"
default:
break
}
})
add TextField to UIAlertController and TextField text Display on UILabel in Swift
let alert = UIAlertController(title: "Alert", message: "", preferredStyle: .alert)
alert.addTextField { (textField) in
textField.placeholder = "First Name"
}
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak alert] (_) in
let textField = alert?.textFields![0]
self.label.text = textField?.text }))
self.present(alert, animated: true, completion: nil)
Swift5
First class conforms to the UITextFieldDelegate , then create new textField property
private var myTextField : UITextField?
// now where u want to add code
let alertContoller = UIAlertController.init(title: "Add", message: "My message to user", preferredStyle: .alert)
alertContoller.addTextField { (textField) in
// make sure your outside any property should be accessed with self here
self.myTextField = textField
//Important step assign textfield delegate to self
self.myTextField?.delegate = self
self.myTextField?.placeholder = self.textFieldPlaceholderText
}
let action = UIAlertAction.init(title: "Ok", style: .default) { action in
print("Alert tapped")
}
alertContoller.addAction(action)
present(alertContoller, animated: true, completion:nil)
Thanks
private func showLoginAlert() {
let loginAlert = UIAlertController(title: "Login Using Credentials", message: nil, preferredStyle: .alert)
loginAlert.view.tintColor = .systemBlue
loginAlert.addTextField { usernameField in
usernameField.font = .systemFont(ofSize: 17.0)
usernameField.placeholder = "Username"
}
loginAlert.addTextField { passwordField in
passwordField.font = .systemFont(ofSize: 17.0)
passwordField.isSecureTextEntry = true
passwordField.placeholder = "Password"
}
let cancelAction = UIAlertAction(title: "Cancel",
style: .destructive,
handler: { _ in
// self.handleUsernamePasswordCanceled(loginAlert: loginAlert)
})
loginAlert.addAction(cancelAction)
let loginAction = UIAlertAction(title: "Login",
style: .default,
handler: { _ in
// self.handleUsernamePasswordEntered(loginAlert: loginAlert)
})
loginAlert.addAction(loginAction)
loginAlert.preferredAction = loginAction
present(loginAlert, animated: true, completion: nil)
}
Swift 5

UialertController to show only once when the var hits an certain Value

so the way my app works is you tap on a cell , a var value gets modified (+1 for example). I've figured out how to get a UIalert to pop when my var reaches a certain value (10). But now everytime I update the var the alert pops. What i would like it to do is to pop when the var hits 10 and stop after that
Heres the code :
if (section1score >= 10){
let alertController: UIAlertController = UIAlertController(title: NSLocalizedString("Section 1 score is over 10", comment: ""),
message: " \(message1)",
preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "OK", style: .Default) {
action -> Void in }
alertController.addAction(OKAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
if (section2score >= 10){
let alertController: UIAlertController = UIAlertController(title: NSLocalizedString("Section 2 Score is over 10", comment: ""),
message: "\(message2)",
preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "OK", style: .Default) {
action -> Void in }
alertController.addAction(OKAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
Setup a Bool to check if the alert has been shown or not. Create the Bool globally and set it to false initially.
var showedAlert = false
func yourFunction() {
if section1score >= 10 && showedAlert == false {
// show alert
showedAlert = true
}
if section2score >= 10 && showedAlert == false {
// show alert
showedAlert = true
}
}
Comparison Operators
You could use a property to keep track of when you show the alert so that once you show it, you won't show it again.
var hasShownAlert: Bool = false
if (section1score >= 10 && !hasShownAlert){
let alertController: UIAlertController = UIAlertController(title: NSLocalizedString("Section 1 score is over 10", comment: ""),
message: " \(message1)",
preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "OK", style: .Default) {
action -> Void in }
alertController.addAction(OKAction)
self.presentViewController(alertController, animated: true, completion: nil)
hasShownAlert = true
}
if (section2score >= 10 && !hasShownAlert){
let alertController: UIAlertController = UIAlertController(title: NSLocalizedString("Section 2 Score is over 10", comment: ""),
message: "\(message2)",
preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "OK", style: .Default) {
action -> Void in }
alertController.addAction(OKAction)
self.presentViewController(alertController, animated: true, completion: nil)
hasShownAlert = true
}

Get input value from TextField in iOS alert in Swift

I'm trying to make an alert message with input, and then get the value from the input. I've found many good tutorials how to make the input text field. but I can't get the value from the alert.
Updated for Swift 3 and above:
//1. Create the alert controller.
let alert = UIAlertController(title: "Some Title", message: "Enter a text", preferredStyle: .alert)
//2. Add the text field. You can configure it however you need.
alert.addTextField { (textField) in
textField.text = "Some default text"
}
// 3. Grab the value from the text field, and print it when the user clicks OK.
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak alert] (_) in
let textField = alert.textFields![0] // Force unwrapping because we know it exists.
print("Text field: \(textField.text)")
}))
// 4. Present the alert.
self.present(alert, animated: true, completion: nil)
Swift 2.x
Assuming you want an action alert on iOS:
//1. Create the alert controller.
var alert = UIAlertController(title: "Some Title", message: "Enter a text", preferredStyle: .Alert)
//2. Add the text field. You can configure it however you need.
alert.addTextFieldWithConfigurationHandler({ (textField) -> Void in
textField.text = "Some default text."
})
//3. Grab the value from the text field, and print it when the user clicks OK.
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { [weak alert] (action) -> Void in
let textField = alert.textFields![0] as UITextField
println("Text field: \(textField.text)")
}))
// 4. Present the alert.
self.presentViewController(alert, animated: true, completion: nil)
Swift 5
You can use the below extension for your convenience.
Usage inside a ViewController:
showInputDialog(title: "Add number",
subtitle: "Please enter the new number below.",
actionTitle: "Add",
cancelTitle: "Cancel",
inputPlaceholder: "New number",
inputKeyboardType: .numberPad, actionHandler:
{ (input:String?) in
print("The new number is \(input ?? "")")
})
The extension code:
extension UIViewController {
func showInputDialog(title:String? = nil,
subtitle:String? = nil,
actionTitle:String? = "Add",
cancelTitle:String? = "Cancel",
inputPlaceholder:String? = nil,
inputKeyboardType:UIKeyboardType = UIKeyboardType.default,
cancelHandler: ((UIAlertAction) -> Swift.Void)? = nil,
actionHandler: ((_ text: String?) -> Void)? = nil) {
let alert = UIAlertController(title: title, message: subtitle, preferredStyle: .alert)
alert.addTextField { (textField:UITextField) in
textField.placeholder = inputPlaceholder
textField.keyboardType = inputKeyboardType
}
alert.addAction(UIAlertAction(title: actionTitle, style: .default, handler: { (action:UIAlertAction) in
guard let textField = alert.textFields?.first else {
actionHandler?(nil)
return
}
actionHandler?(textField.text)
}))
alert.addAction(UIAlertAction(title: cancelTitle, style: .cancel, handler: cancelHandler))
self.present(alert, animated: true, completion: nil)
}
}
In Swift5 ans Xcode 10
Add two textfields with Save and Cancel actions and read TextFields text data
func alertWithTF() {
//Step : 1
let alert = UIAlertController(title: "Great Title", message: "Please input something", preferredStyle: UIAlertController.Style.alert )
//Step : 2
let save = UIAlertAction(title: "Save", style: .default) { (alertAction) in
let textField = alert.textFields![0] as UITextField
let textField2 = alert.textFields![1] as UITextField
if textField.text != "" {
//Read TextFields text data
print(textField.text!)
print("TF 1 : \(textField.text!)")
} else {
print("TF 1 is Empty...")
}
if textField2.text != "" {
print(textField2.text!)
print("TF 2 : \(textField2.text!)")
} else {
print("TF 2 is Empty...")
}
}
//Step : 3
//For first TF
alert.addTextField { (textField) in
textField.placeholder = "Enter your first name"
textField.textColor = .red
}
//For second TF
alert.addTextField { (textField) in
textField.placeholder = "Enter your last name"
textField.textColor = .blue
}
//Step : 4
alert.addAction(save)
//Cancel action
let cancel = UIAlertAction(title: "Cancel", style: .default) { (alertAction) in }
alert.addAction(cancel)
//OR single line action
//alert.addAction(UIAlertAction(title: "Cancel", style: .default) { (alertAction) in })
self.present(alert, animated:true, completion: nil)
}
For more explanation https://medium.com/#chan.henryk/alert-controller-with-text-field-in-swift-3-bda7ac06026c
Swift version: 5.+
Create a new TextField variable in current scope and assign it to alertTextField in alert.addTextField completion handler. Use textField's value inside UIAlertAction completion handler.
#IBAction func addButtonPressed(_ sender: UIBarButtonItem) {
//Variable to store alertTextField
var textField = UITextField()
let alert = UIAlertController(title: "Add new item", message: "", preferredStyle: .alert)
alert.addTextField { alertTextField in
alertTextField.placeholder = "Create new item"
//Copy alertTextField in local variable to use in current block of code
textField = alertTextField
}
let action = UIAlertAction(title: "Add item", style: .default) { action in
//Prints the alertTextField's value
print(textField.text!)
}
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
let ac = UIAlertController(title: "title", message: "message", preferredStyle: .alert)
ac.addTextField()
let submitAction = UIAlertAction(title: "Submit", style: .default) { [weak self, weak ac] action in
guard let wordToget = ac?.textFields?[0].text else { return }
//here you can do what you need like
print(wordToget)
}
ac.addAction(submitAction)
present(ac, animated: true)

Resources