ios - Passing data to another ViewController in Swift -


before begin, let me have taken @ popular post on matter: passing data between view controllers

my project on github https://github.com/model3volution/tipme

i inside of uinavigationcontroller, using pushsegue.

i have verified ibaction methods linked , segue.identifier corresponds segue's identifier in storyboard.

if take out prepareforsegue: method segue occurs, without data updating.

my specific error message is: could not cast value of type 'tipme.facesviewcontroller' (0x10de38) 'uinavigationcontroller' (0x1892e1c).

override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { // new view controller using segue.destinationviewcontroller.     if segue.identifier == "tofacesvc" {         let navcontroller:uinavigationcontroller = segue.destinationviewcontroller as! uinavigationcontroller         let facesvc = navcontroller.topviewcontroller as! facesviewcontroller         facesvc.balancelabel.text = "balance before tip: $\(balancedouble)"     }  } 

below screenshot code , error. side notes: using xcode 6.3, swift 1.2 screen shot code , error

a couple of things:

1: change prepareforsegue

if segue.identifier == "tofacesvc" {     let facesvc = segue.destinationviewcontroller as! facesviewcontroller     facesvc.text = "balance before tip: $\(balancedouble)" } 

2: add string variable facesviewcontroller

var text:string! 

3: change facesviewcontroller viewdidload

override func viewdidload() {     super.viewdidload()      balancelabel.text = text } 

the reasons changes: segue destinationviewcontroller actual facesviewcontroller transition -> no need navigationcontroller shenanigans. alone remove "case error", occur due unwrapping nil value because try access balancelabel not have been set yet. therefore need create string variable hold string want assign , assign text in viewdidload - @ point uilabel assigned.

proof works:

enter image description here

4: if want display 2 decimal places balance might change string creation (following https://stackoverflow.com/a/24102844/2442804):

facesvc.text =  string(format: "balance before tip: $%.2f", balancedouble) 

resulting in:

enter image description here


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -