Eth*_*hao 5 xcode uiviewcontroller ios swift swift3
我有来自视图控制器的数据,我想传递另一个视图控制器,但我将其设置为模态呈现,所以我在它们之间有一个导航控制器。如何将数据从第一个视图控制器通过导航控制器传递到第二个视图控制器?
我在第一个视图控制器中有这段代码:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "presentPopup"
{
let destViewController = segue.destination as! NavigationViewController
destViewController.myData2 = myData
}
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
Run Code Online (Sandbox Code Playgroud)
然后导航控制器中的这段代码:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destViewController = segue.destination as! SecondViewController
destViewController.myData3 = myData2
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用。
您可以在第一个 ViewController 中使用它:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "presentPopup"
{
let destViewController = segue.destination as! NavigationViewController
let secondViewcontroller = destViewController.viewcontrollers.first as! SecondViewcontroller
secondViewcontroller.myData2 = myData
}
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
Run Code Online (Sandbox Code Playgroud)