访问另一个视图控制器中的变量 - Xcode 8.0 Swift 3.0

时间:2016-10-16 19:33:08

标签: swift xcode uiviewcontroller segue

如何在一个VC上设置变量然后在另一个VC上访问它?我知道你需要使用prepare for segue但我想在VC出现时访问变量。到目前为止,这是我的代码:

override func viewDidAppear(_ animated: Bool) {

    print("viewDidAppear")

    func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        print("prepare for segue")

        let variableUnwind = ("StackOverFlow")

        if segue.identifier == "toFirstViewController"  {

            let hello = segue.destination as! AddItemViewController

            print(hello.username)



        }

    }

}

我知道为segue做准备意味着该函数中的动作将在segue之前发生但我不知道如何消除对segue的准备而不会丢失该函数中会发生的事情。

基本上,我想在VC2加载时在VC2上设置一个变量并在VC1上访问它。如果它有助于我使用unwind segue从VC2转到VC1。

1 个答案:

答案 0 :(得分:0)

你需要做

你好。你的变量名是什么=你在VC2上创建的新变量

声明hello变量

之后

希望这会有所帮助

override func viewDidAppear(_ animated: Bool) {
print("viewDidAppear")

func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    print("prepare for segue")

    let variableUnwind = ("StackOverFlow")

    if segue.identifier == "toFirstViewController"  {

        let hello = segue.destination as! AddItemViewController

hello.variableName = newVariableName

    }

}

}

相关问题