ios - How can I retrieve input from an alert box in Swift? -
how can retrieve input alert box in swift? don't understand why code isn't working. i'm c++ programmer i'm new swift. reason when print line says: "new style added is:" , that's there is. won't print out user has typed in textbox reason.. here code
// generate text field user input func generatetextfield() { //1. create alert controller. var tempstyle = ""; var alert = uialertcontroller(title: "add new style", message: "enter name of new hairstyle below", preferredstyle: .alert); //2. add text field. can configure need. alert.addtextfieldwithconfigurationhandler({ (textfield) -> void in textfield.placeholder = "your new hairstyle goes here.."; }) //3. grab value text field, , print when user clicks ok. alert.addaction(uialertaction(title: "ok", style: .default, handler: { (action) -> void in let textfield = alert.textfields![0] uitextfield tempstyle = textfield.text!; })) // 4. present alert. self.presentviewcontroller(alert, animated: true, completion: nil) print("new style added is: " + tempstyle); }
try adding print("new style added is: " + tempstyle)
tempstyle = textfield.text!
. looks print command not being called in right place. tempstyle knows is equal "", explain getting "new style added is:". have add code function variable changed or make var tempstyle = "" class-wide variable. in case, add variable outside of function. if make class-wide variable, leave print("new style added is: " + tempstyle)
need make print("new style added is: " + self.tempstyle)
, referring face tempstyle created in class (that viewcontroller). also, don't need ";" in swift, i'm guessing it's force of habit objective c!
Comments
Post a Comment