2017-04-12 66 views
0
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在這段代碼中爲零。 請教我。如何在2個viewControllers之間傳遞字符串?

回答

0

首先將值分配給textObject然後呈現的viewController:

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