【问题标题】:how to passdata between storyboard如何在故事板之间传递数据
【发布时间】:2020-08-27 20:45:26
【问题描述】:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator){
    if UIDevice.current.orientation.isPortrait{
        let secondView: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let main = secondView.instantiateViewController(withIdentifier: "ViewController") as! ViewController
        main.modalPresentationStyle = .fullScreen
        self.present(main, animated: true, completion: nil)

    }

    if UIDevice.current.orientation.isLandscape{

        self.dismiss(animated: true, completion: nil)

    }

}

我使用此代码在两个不同故事板的视图控制器之间切换。如何在两个不同的故事板中的这些视图控制器之间传递数据。 我试过“main.instanceData = myData before the present”好像可以,但是myData的初始值为nil,所以这行代码执行的时候会显示“Thread 1: Fatal error: Unexpectedly found nil while隐式展开可选值”

【问题讨论】:

  • 在这种情况下“不起作用”是什么意思? (请显示它的实际代码不起作用。)
  • 为什么需要在storyboards之间传递数据? Storyboard 只是一组视图控制器。数据应该为视图控制器来回传递。

标签: ios swift xcode viewcontroller


【解决方案1】:

试试这个。您可以像这样直接将数据传递给 viewController:

let secondView: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let main = secondView.instantiateViewController(withIdentifier: "ViewController") as! ViewController
main.data = yourdata
main.modalPresentationStyle = .fullScreen
self.present(main, animated: true, completion: nil)

【讨论】:

  • 这个我试过了,但是不行。我认为这是因为我的视图控制器位于两个不同的故事板中。
  • 我认为这与不同的故事板无关。如果您使用 Segue,则无法在两个故事板之间传递数据。
  • 我又试了你的方法,好像可行,但我的数据初始值为nil。所以当这行代码执行时,会显示“Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value”
猜你喜欢
  • 2012-01-25
  • 2013-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-03
  • 2012-01-15
  • 2018-11-20
  • 1970-01-01
相关资源
最近更新 更多