3

Before I begin, let me say that I have taken a look at a popular post on the matter: Passing Data between View Controllers

My project is on github https://github.com/model3volution/TipMe

I am inside of a UINavigationController, thus using a pushsegue.

I have verified that my IBAction methods are properly linked up and that segue.identifier corresponds to the segue's identifier in the storyboard.

If I take out the prepareForSegue: method then the segue occurs, but obviously without any data updating.

My specific error message is: Could not cast value of type 'TipMe.FacesViewController' (0x10de38) to 'UINavigationController' (0x1892e1c).

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the 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 is a screenshot with the code and error. side notes: using Xcode 6.3, Swift 1.2 screen shot with code and error

Community
  • 1
  • 1
Focus Otter
  • 359
  • 1
  • 4
  • 10
  • As another comment already suggested: include code within your question. I did that for you now because providing the project to download somewhere enables other users to help you much easier. I like that :) +1 – luk2302 Jun 14 '15 at 15:59

1 Answers1

3

A couple of things:

1: change your prepareForSegue to

if segue.identifier == "toFacesVC" {
    let facesVC = segue.destinationViewController as! FacesViewController
    facesVC.text = "Balance before tip: $\(balanceDouble)"
}

2: add a string variable to your FacesViewController

var text:String!

3: change the FacesViewController viewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()

    balanceLabel.text = text
}

The reasons for all the changes: the segue destinationViewController is the actual FacesViewController you transition to -> no need for the navigationController shenanigans. That alone will remove the "case error", but another will occur due to unwrapping a nil value because you try to access the balanceLabel which will not have been set yet. Therefore you need to create a string variable to hold the string you actually want to assign and then assign that text in the viewDidLoad - at the point where the UILabel is actually assigned.

Proof that it works:

enter image description here

4: If you want display two decimal places for the balance you might change the String creation to something like (following https://stackoverflow.com/a/24102844/2442804):

facesVC.text =  String(format: "Balance before tip: $%.2f", balanceDouble)

resulting in:

enter image description here

Community
  • 1
  • 1
luk2302
  • 55,258
  • 23
  • 97
  • 137
  • I think he wants it to be "$4.00" instead of "$4.0". I don't know how to do it in Swift, but in Objective C it's this to return an `NSString` object: `[NSString stringWithFormat:@"Balance before tip: $.2f", balanceDouble]` – DDPWNAGE Jun 14 '15 at 15:58
  • might be, don´t know, has nothing to do with his actual question though. And I do not know any way to do that within the string literal `\(...)` – luk2302 Jun 14 '15 at 16:01
  • I know it didn't have much to do with the OP's question but I was just looking at the screenshot and the "$4.0" annoyed me a bit. – DDPWNAGE Jun 14 '15 at 16:03
  • @DDPWNAGE Okay, for completion sake I will include it ;) – luk2302 Jun 14 '15 at 16:06
  • @luk2302 Thanks a ton! I suppose the only question I'm left with is why would one access the navigation controller (as I did) if it seems--at least in this case, unnecessary? My perception was that whenever a view controller was embedded in a navigation controller, you had to access the navigation controller first...though I now see that's not the case. Care to enlighten? – Focus Otter Jun 14 '15 at 17:35
  • 1
    you only have to access the navigation controller if you push a navigation Controller, e.g. from within a tabbar controller. while you are inside a navigation controller / its navigation stack, you will always access the actual controller that will be presented. if you need more info than that i sadly can´t help you ... – luk2302 Jun 14 '15 at 17:38
  • @luk2302 ah, answers my question perfectly! Thanks! – Focus Otter Jun 14 '15 at 18:08