简体   繁体   English

将数据从ViewController传递回根ViewController?

[英]Pass data from ViewController back to root ViewController?

I wonder how I can pass data between VC3 and VC1. 我想知道如何在VC3和VC1之间传递数据。

What I have is a root view(VC1). 我所拥有的是一个根视图(VC1)。 Then you enter a tableView and if you pick an item you will enter tableView2(VC3) - category - sub category picker. 然后输入tableView,如果选择一个项目,则将输入tableView2(VC3)-类别-子类别选择器。

What I have done so far is to declare this in VC1: 到目前为止,我所做的就是在VC1中声明:

@IBAction func unwindToVC1(segue: UIStoryboardSegue) {

 }

Then in story board I control-dragged the yellow button to the red button in VC3 and choose "unwindToVC1" as the action, then I named the segue. 然后在故事板中,我将VC3中的黄色按钮控制拖动到红色按钮,并选择“ unwindToVC1”作为动作,然后命名为segue。

I then fire the segue in VC3 by performSegueWithIdentifier() 然后,我通过performSegueWithIdentifier()在VC3中触发segue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "Vc1" {
            let toView = segue.destinationViewController as! myRootVc

            let test = "test string"

            toView.test = test

        }
    }

It almost works, I end up in VC1 but the passed value shows up as nil in VC1, even though I have it declared in vc1 since before: 几乎可以正常工作,我最终在VC1中运行,但是即使在之前的vc1中已经声明了,所传递的值在VC1中也显示为nil:

var test: String? var test:字符串?

you are using segues to pass data between VC! 您正在使用segue在VC之间传递数据! and VC3 an when you have selcted anitem from sub category. 和VC3,则从子类别中选择了项目。 you can use Delegates 您可以使用代表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM