首页 > 解决方案 > 在不使用 Storyboard Reference 的情况下转到另一个 Storyboard?

问题描述

我的应用程序中有一个按钮,单击时会发出警报,并且仅当用户单击“确定”时-我想将它们发送到另一个故事板。是否可以在 ViewController 中执行此操作?没有故事板参考?代码会是什么样子?

标签: swiftxcode

解决方案


根据您评论中更新的详细信息,您需要在情节提要中实例化您的 viewController 并手动执行导航,如下所示:

let alert = UIAlertController(title: "Alert!", message: "Do the thing", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default) { (action) in
    let storyboard = UIStoryboard(name: "Bussturen", bundle: nil)
    let viewController = storyboard.instantiateViewController(identifier: "BussturenViewController") as! BussturenViewController
    //Do any more setup you might need to do for your viewController before navigation
    self.navigationController?.pushViewController(viewController, animated: true)
}
alert.addAction(action)
self.present(alert, animated: true, completion: nil)

另请注意,这假设在故事板的身份检查器中,您将BussturenViewController设置为“BussturenViewController”


推荐阅读