UIAlertView + TextField = Error?

252 views Asked by At

I am making a application so data can be saved to a Table View and I wanted to use the UIAlertView Controller in a way so people can use it to create a new Cell (task) but I know how to make a textField in a UIAlertView but how do use the textField data and use it in a UIAlertView Action button!

@IBAction func addNewTask() {

        var alertController = UIAlertController(title: "Add A New Task", message: "Add The Correct Information To Add A New Task!", preferredStyle: .Alert)

        // Create the actions
        var Exit = UIAlertAction(title: "Add Task", style: UIAlertActionStyle.Default) {
            UIAlertAction in
            NSLog("OK Pressed")


        }
        var cancelAction = UIAlertAction(title: "Exit App", style: UIAlertActionStyle.Cancel) {
            UIAlertAction in
            NSLog("User Exited The App!")
            exit(2)

        }

        alertController.addTextFieldWithConfigurationHandler { (textField: UITextField!) -> Void in
            // Here you can configure the text field (eg: make it secure, add a placeholder, etc)

        }

        // Add the actions
        alertController.addAction(Exit)

        alertController.addAction(cancelAction)

        // Present the controller
        self.presentViewController(alertController, animated: true, completion: nil)



    }

This is the code but how can I use the data from the alertController.addTextFieldWithConfigureationHandler and put it in the Exit Action! So the main thing I want this to do just know is when the user types in the textbox and clicks Exit a label will change to that text from the textField but without using a viewController! I am using Swift, Xcode 6.3.1

Thanks,

George Barlow

p.s. Sorry for the longwinded question!

3

There are 3 answers

0
Kumuluzz On BEST ANSWER

In your UIAlertAction called Exit (btw, you should consider changing the name to something like exit or exitAction), you can access the UITextField and its contents in the following way:

// Create the actions
var Exit = UIAlertAction(title: "Add Task", style: UIAlertActionStyle.Default) {
    UIAlertAction in
    let loginTextField = alertController.textFields![0] as! UITextField
    let inputText = loginTextField.text
    println("The input text is: \(inputText)")
    NSLog("OK Pressed")
}
0
robotwholearned On

" . .. you can also add text fields to the alert interface. The alert controller lets you provide a block for configuring your text fields prior to display. The alert controller maintains a reference to each text field so that you can access its value later." https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/index.html#//apple_ref/occ/cl/UIAlertController

There is a textfield property on the UIAlertViewController that you can access.

alertController.textFields

The text fields are added to this array in the same order that you added them to the alert controller.

Inside your completion for the exit action:

UITextField *taskTextField = alertController.textFields.firstObject

*Edit: in swift:

if let taskTextField = alertController.textFields![0] as! UITextField {
    let taskText = taskTextField.text
    ...
}
0
Himanshu gupta On

You can do something like this:

class ViewController: UIViewController {

        @IBOutlet weak var myLabel: UILabel!
        var mytextField: UITextField!

        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.

        }

        override func viewDidAppear(animated: Bool) {
            addNewTask()
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }


        func addNewTask() {
            var alertController = UIAlertController(title: "Add A New Task", message: "Add The Correct Information To Add A New Task!", preferredStyle: .Alert)

            // Create the actions
            var Exit = UIAlertAction(title: "Add Task", style: UIAlertActionStyle.Default) {
                UIAlertAction in
                NSLog("OK Pressed")

                self.myLabel.text = self.mytextField.text


            }
            var cancelAction = UIAlertAction(title: "Exit App", style: UIAlertActionStyle.Cancel) {
                UIAlertAction in
                NSLog("User Exited The App!")
                exit(2)

            }

            alertController.addTextFieldWithConfigurationHandler { (textField: UITextField!) -> Void in
                // Here you can configure the text field (eg: make it secure, add a placeholder, etc)

                self.mytextField = textField
            }

            // Add the actions
            alertController.addAction(Exit)

            alertController.addAction(cancelAction)

            // Present the controller
            self.presentViewController(alertController, animated: true, completion: nil)
        }


    }