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


i'm using uialertcontroller display textfield input phone number. when click ok, textfield's text contents stored in variable can conduct operation it. here's 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 attempt access popuptext , print contents, seems empty, console shows nothing. there way access textfield's contents, or have identifier it? (uialertcontroller doesn't seem let me try "alc.textfield.text) i'm guessing how i'm handling "config" wrong, still don't know how access controller's text field. appreciated. thanks!

there few problems way you're going this.

setting text field

    var popuptext: string = ""     func config(textfield: uitextfield!)->void{         popuptext = textfield.text     }     ...     alc.addtextfieldwithconfigurationhandler(config) 

the config handler you're passing addtextfieldwithconfigurationhandler configuring new text field, looks you're trying access value. text field brand new, empty, don't think useful you. if need change attributes of text field, correct location, otherwise pass nil addtextfield....

    func config(textfield: uitextfield!)->void{         textfield.textcolor = uicolor.redcolor()     } 

accessing 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 empty string variable, isn't linked either text field, alert action, or alert controller in way. want access text field itself, can find using alc.textfields[0]. second, looks you're hoping store value of text field in popuptext, that's local variable method , isn't going accessible anywhere else. depending on want it, suggest storing phone number in instance property of class. in case, want more this:

    alc.addaction(uialertaction(title: "submit", style: uialertactionstyle.default, handler:{ uialertaction in         println(alc.textfields[0].text)         self.performseguewithidentifier("popupsegue", sender: alc)     })) 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -