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。
请教我。

最佳答案

首先为textObject分配值,然后显示viewController:

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

关于swift - 如何在2个viewController之间传递字符串?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43359570/

10-10 22:33