7

In my application I want a alert with a textfield. After clicking on "Done" I want to save the textfield input in a String. After clicking on "Cancel" I want only to close the alert. I've created my alert like this:

 var alert = UIAlertView() alert.title = "Enter Input" alert.addButtonWithTitle("Done") alert.alertViewStyle = UIAlertViewStyle.PlainTextInput alert.addButtonWithTitle("Cancel") alert.show() let textField = alert.textFieldAtIndex(0) textField!.placeholder = "Enter an Item" println(textField!.text) 

The alert looks like this:

My alert

I want to know how to get the text from the textfield, and how to create events for the "Done" button and the "Cancel" button.

4
  • 1
    You need to implement the appropriate UIAlertViewDelegate methods (and set the delegate property of the alert view). Commented May 7, 2015 at 19:57
  • can someone give me an example with this "UIAlerrViewDelegate" ? Commented May 7, 2015 at 20:38
  • 1
    Please do a little searching. Commented May 7, 2015 at 20:39
  • I searched about this topic, now I've a way to check which button was pressed. But I haven't found a running solution for getting the text from the textfield. So please help me about this! Commented May 8, 2015 at 19:12

4 Answers 4

28

You may go with UIAlertController instead of UIAlertView.

I've already implemented and tested too using UIAlertController for what you actually want. Please try the following code

 var tField: UITextField! func configurationTextField(textField: UITextField!) { print("generating the TextField") textField.placeholder = "Enter an item" tField = textField } func handleCancel(alertView: UIAlertAction!) { print("Cancelled !!") } var alert = UIAlertController(title: "Enter Input", message: "", preferredStyle: .Alert) alert.addTextFieldWithConfigurationHandler(configurationTextField) alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler:handleCancel)) alert.addAction(UIAlertAction(title: "Done", style: .Default, handler:{ (UIAlertAction) in print("Done !!") print("Item : \(self.tField.text)") })) self.presentViewController(alert, animated: true, completion: { print("completion block") }) 
Sign up to request clarification or add additional context in comments.

3 Comments

Exactly what I'm searching for.
@DharmeshKheni Glad that it helped you!
Nice Code exactly looking for this..:)
2

You will have to implement the UIAlertViewDelegate's

optional func alertView(_ alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) 

Comments

2

For SWIFT 3

@IBAction func ForgotPassword(_ sender: Any) { let alertController = UIAlertController(title: "Email?", message: "Please input your email:", preferredStyle: .alert) let confirmAction = UIAlertAction(title: "Confirm", style: .default) { (_) in if let field = alertController.textFields![0] as? UITextField { // store and use entered data } else { print("please enter email id") } } let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in } alertController.addTextField { (textField) in textField.placeholder = "Email" } alertController.addAction(confirmAction) alertController.addAction(cancelAction) self.present(alertController, animated: true, completion: nil) } 

I hope it will help someone else :)

Comments

0

For iOS 8+ you should use UIAlertController instead of UIAlertView. To support iOS 7 you should implement (UIAlertViewDelegate):

func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) { //... let textField = alertView.textFieldAtIndex(0) } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.