How do I segue values when my ViewController is em

2020-03-30 02:45发布

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

1条回答
ゆ 、 Hurt°
2楼-- · 2020-03-30 03:13

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

查看更多
登录 后发表回答