Problems with getting text from UIAlertView textfield

Pascal Ackermann

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.

iRiziya

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")
    })

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java GUI: Getting text from a textfield

From Dev

Saving a text from UIAlertView

From Dev

Problems getting text from control on another window

From Dev

Problems getting text from control on another window

From Dev

AutoCapitalization of TextField of UIAlertView

From Dev

AutoCapitalization of TextField of UIAlertView

From Dev

iOS 7 XCode 5 - UIAlertView with TextField - How to pull text into a Label or NSString?

From Dev

In UIAlertView trying to view two textfield. Its showing error bounds of the array of text fields iOS7

From Dev

How to add a TextField to UIAlertView in Swift

From Dev

Problems in getting an array from NSUserDefaults

From Dev

Problems getting value from intent

From Dev

Problems getting the control from GridViewRow

From Dev

UIAlertView category : getting error

From Dev

UIAlertView delegate not getting called?

From Dev

Getting the value textfield from Form based UITableView

From Dev

Getting data from a textfield inside a prototype uicollectionviewcell

From Dev

Getting data from textfield in a dynamicallly created table

From Dev

partial, getting some data from textField

From Dev

UIAlertView with multiple text inputs?

From Dev

UIAlertView with multiple text inputs?

From Dev

How to get the input text from the TextField in an alert

From Dev

How to copy text to Clipboard from TextField in Android

From Dev

use text from textField for naming - maya python

From Dev

ios get text value from textfield in tableViewCell

From Dev

access textfield text from another viewController

From Dev

Gather textfield text from a tableview cell (Swift)

From Dev

Add TextField to UIAlertView in Swift that saves to a TableView controller

From Dev

UIAlertView with textfield and three buttons issue in ios 6

From Dev

UIAlertView with textfield and three buttons issue in ios 6

Related Related

  1. 1

    Java GUI: Getting text from a textfield

  2. 2

    Saving a text from UIAlertView

  3. 3

    Problems getting text from control on another window

  4. 4

    Problems getting text from control on another window

  5. 5

    AutoCapitalization of TextField of UIAlertView

  6. 6

    AutoCapitalization of TextField of UIAlertView

  7. 7

    iOS 7 XCode 5 - UIAlertView with TextField - How to pull text into a Label or NSString?

  8. 8

    In UIAlertView trying to view two textfield. Its showing error bounds of the array of text fields iOS7

  9. 9

    How to add a TextField to UIAlertView in Swift

  10. 10

    Problems in getting an array from NSUserDefaults

  11. 11

    Problems getting value from intent

  12. 12

    Problems getting the control from GridViewRow

  13. 13

    UIAlertView category : getting error

  14. 14

    UIAlertView delegate not getting called?

  15. 15

    Getting the value textfield from Form based UITableView

  16. 16

    Getting data from a textfield inside a prototype uicollectionviewcell

  17. 17

    Getting data from textfield in a dynamicallly created table

  18. 18

    partial, getting some data from textField

  19. 19

    UIAlertView with multiple text inputs?

  20. 20

    UIAlertView with multiple text inputs?

  21. 21

    How to get the input text from the TextField in an alert

  22. 22

    How to copy text to Clipboard from TextField in Android

  23. 23

    use text from textField for naming - maya python

  24. 24

    ios get text value from textfield in tableViewCell

  25. 25

    access textfield text from another viewController

  26. 26

    Gather textfield text from a tableview cell (Swift)

  27. 27

    Add TextField to UIAlertView in Swift that saves to a TableView controller

  28. 28

    UIAlertView with textfield and three buttons issue in ios 6

  29. 29

    UIAlertView with textfield and three buttons issue in ios 6

HotTag

Archive