我正在尝试制作一个在每个 View 上都有一个主页按钮的应用程序,但我正在寻找一种在单击此按钮时导航到主屏幕的方法,而不需要为每个屏幕创建“物理”链接 Storyboard到主屏幕。
我尝试使用此代码:
@IBAction func btnGoInicio(_ sender: Any) {
let page = MainVC()
present(page, animated: true, completion: nil)
}
但是黑屏崩溃,有人知道我该怎么做吗?提前致谢!
最佳答案
您必须从 Storyboard 中实例化 View Controller ,如下所示:
@IBAction func btnGoInicio(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil) // If you have other storyboard instead of Main, use it
if let page=self.storyboard?.instantiateViewControllerWithIdentifier("YOU_IDENTIFIER") as! MainVC{
//You MUST SET THE VIEW CONTROLLER IDENTIFIER "YOU_IDENTIFIER" FROM INSPECTOR INTO STORYBOARD
self.present(page, animated: true, completion: nil)
}
}
关于ios - 没有 Storyboard 的 Segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52524596/