How do I segue values when my ViewController is em

2020-03-30 03:13发布

问题:

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...

回答1:

casting it as ViewController is working fine for me.

var dest = segue.destinationViewController as! ViewController

And your segue connection should be :