xcode - Why am I receiving these errors when trying to pass a variable on a segue in swift? -


i trying build upon answer given here. trying simple - want text field can enter text into. press go button , takes new view , replaces text on label on page whatever user entered in box. code using on first page.

import uikit  class viewcontroller: uiviewcontroller {      @iboutlet var entry: uitextfield!      let dictionary = entry.text // line 7 error       override func viewdidload() {         super.viewdidload()         // additional setup after loading view, typically nib.     }      override func didreceivememorywarning() {         super.didreceivememorywarning()         // dispose of resources can recreated.     }      override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) {         if segue.identifier == "viewtwo"         {             if let destinationvc = segue.destinationviewcontroller as? viewtwo{                 destinationvc.dictionary = self.dictionary // line 24 error             }         }     }      @ibaction func gotoviewtwo(sender: anyobject) {         performseguewithidentifier("viewtwo", sender: self)     }  } 

i including code first view because know code second view working.

i didn't encounter error until tried use text field - before when had pre-choses text transfer on worked. before, instead of having let dictionary = entry.text had let dictionary = "foo" , worked.

so question same thing have text field instead of pre-chosen text - want know why code didn't work before.

the errors got on line 7 (i have labeled lines above had errors) - 'viewcontroller.type' not have member names 'entry' , there error on line 24 suspect related error , fixed if error fixed. incase though, error on line 24 was: 'viewcontroller.type' not have member names 'dictionary'

thank you.

you should set dictionary var dictionary = "" in declaration. use var instead of let here, can change value of dictionary later.

then inside @ibaction func gotoviewtwo(sender: anyobject){} method, set self.dictionary = entry.text

 @ibaction func gotoviewtwo(sender: anyobject) {         dictionary = entry.text         performseguewithidentifier("viewtwo", sender: self)     } 

alternatively, can following inside prepareforsegue() method. way, dont need declare dictionary hold text value of uitextfield, can pass text value entry second view controller's dictionary variable.

override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) {         if segue.identifier == "viewtwo"         {             if let destinationvc = segue.destinationviewcontroller as? viewtwo{                 destinationvc.dictionary = self.entry.text             }         }     } 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -