How do I segue values when my ViewController is embedded in an UINavigationController?
Asked Answered
P

1

2

I have two view controllers that send values back and forth via segues and the prepareForSegue method. This has been working out perfectly for me until I decided to embed my ViewController inside a Navigation Controller.

If you look at the method below you can see the problem is that my destinationViewController is no longer ViewController... its my newly created UINavigationController.

TimeViewController.swift (secondary controller)

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        var dest = segue.destinationViewController as! ViewController
        var min = selectedMinute * 60
        output = min + selectedSecond

        dest.returnedLabel = recievedString

        if cancelled == true {
            dest.returnedValue = 0
        } else {
            dest.returnedValue = output
        }

    }

Once my ViewController is embedded in the UINavigationController I get this error:

Could not cast value of type 'UINavigationController' (0x1088ff698) to 'deleteCoreData.ViewController' (0x106a1f9b0).

So, if I cast my destinationViewController as a UINavigationController I rightfully lose access to the variables that I had previously. These variables are how I persist the data between the controllers.

So my question is:

How do I segue values when my ViewController is embedded in an UINavigationController?

Update: This is what my storyboard looks like...

enter image description here

Parrisch answered 15/4, 2015 at 3:55 Comment(0)
N
3

casting it as ViewController is working fine for me.

var dest = segue.destinationViewController as! ViewController

And your segue connection should be :

enter image description here

Nickelodeon answered 15/4, 2015 at 4:7 Comment(7)
the code posted is from my TimeViewController, I'm segueing back to the ViewController. I'll label my code.Parrisch
It compiles and runs but when I select a time in my TimeViewController and attempt to pass the variables back it crashes. Saying "Could not cast value of type 'UINavigationController'"Parrisch
I'll give it a shot, I've added a picture of my storyboard btwParrisch
hmmm....ohhkay. This is odd. I've tried this several times and this time it works.Parrisch
you're right in your implementation. My Xcode is acting buggy. I've removed the segues and added them back to my ViewController and now it works as it did before while keeping the Navigation Controller. Thank you!Parrisch
if you add that image of your storyboard connected properly to your answer I'll mark it as the answer to this question.Parrisch
thank you! I probably wouldn't have tried setting my segues back to the view controller again if you hadn't told me to. I would've been at this all night!Parrisch

© 2022 - 2024 — McMap. All rights reserved.