如何在2个viewControllers之间传递字符串?

时间:2017-04-12 03:08:58

标签: swift3 textview

var textToPass: String = ""

@IBAction func modalAction(_ sender: Any) {

        textToPass = textView.text
        print(textToPass)

        let storyBoard = UIStoryboard(name: "Main", bundle: nil)

        if let secondVC = storyBoard.instantiateViewController(withIdentifier: "SecondVC") as? SecondViewController {

            present(secondVC, animated: true, completion: nil)

            secondVC.textObject = textToPass  // textObject = String?
}

您好。 我不知道为什么secondVC.textObject在这段代码中是nil。 请教我。

1 个答案:

答案 0 :(得分:0)

首先为textObject赋值,然后呈现viewController:

if let secondVC = storyBoard.instantiateViewController(withIdentifier: "SecondVC") as? SecondViewController {
        secondVC.textObject = textToPass 
        present(secondVC, animated: true, completion: nil)
}