简体   繁体   中英

Swift: Accessing the textfield/segue to a new ViewController in UIAlertController

I'm using UIAlertController to display a textField to input a phone number. When I click OK, I would like the textfield's text contents to be stored in a variable so I can conduct some operation with it. Here's the code:

@IBAction func popup(sender : AnyObject) {
    var popupText: String = ""
    func config(textField: UITextField!)->Void{
        popupText = textField.text
    }

    var alc: UIAlertController = UIAlertController(title: "Phone", message: "Please enter phone #: ", preferredStyle: UIAlertControllerStyle.Alert)
    alc.addTextFieldWithConfigurationHandler(config)
    alc.addAction(UIAlertAction(title: "Submit", style: UIAlertActionStyle.Default, handler:{ UIAlertAction in
            print(popupText)
            self.performSegueWithIdentifier("popupSegue", sender: alc)


        }))
    alc.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
    self.presentViewController(alc, animated: true, completion: nil)
}

When I attempt to access popupText and print its contents, it seems to be empty, as the console shows nothing. Is there a way to access this textField's contents, or even have an identifier for it? (UIAlertController doesn't seem to let me try "alc.textField.text) I'm guessing how I'm handling with "config" is wrong, but I still don't know how to access this controller's text field. Any help would be appreciated. Thanks!

There are a few problems with the way you're going about this.

Setting up the text field

    var popupText: String = ""
    func config(textField: UITextField!)->Void{
        popupText = textField.text
    }
    ...
    alc.addTextFieldWithConfigurationHandler(config)

The config handler you're passing to addTextFieldWithConfigurationHandler is for configuring the new text field, but it looks like you're trying to access its value. The text field is brand new, so it will be empty, which I don't think is very useful to you. If you need to change some attributes of the text field, this is the correct location, otherwise just pass nil to addTextField... .

    func config(textField: UITextField!)->Void{
        textField.textColor = UIColor.redColor()
    }

Accessing the text field's value

    alc.addAction(UIAlertAction(title: "Submit", style: UIAlertActionStyle.Default, handler:{ UIAlertAction in
        print(popupText)
        self.performSegueWithIdentifier("popupSegue", sender: alc)
    }))

Two problems here -- popupText is simply an empty String variable, it isn't linked to either the text field, the alert action, or the alert controller in any way. You want to access the text field itself, which you can find using alc.textFields[0] . Second, it looks you're hoping to store the value of the text field in popupText , but that's a local variable to this method and isn't really going to be accessible anywhere else. Depending on what you want to do with it, I would probably suggest storing the phone number in an instance property of your class. In any case, you want something more like this:

    alc.addAction(UIAlertAction(title: "Submit", style: UIAlertActionStyle.Default, handler:{ UIAlertAction in
        println(alc.textFields[0].text)
        self.performSegueWithIdentifier("popupSegue", sender: alc)
    }))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM