如何在情节提要之间传递数据

问题描述

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)

    }

}

我使用此代码在两个不同的情节提要的viewcontroller之间切换。我如何在两个不同故事板中的那些视图控制器之间传递数据。 我尝试过“ main.instanceData = myData在当前之前”,这似乎可行,但是myData的初始值为nil,因此当执行此行代码时,它将显示“线程1:致命错误:意外地发现nil,而隐式展开一个可选值”

解决方法

只需尝试一下。您可以像这样将数据直接传递给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)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...