我正在尝试修复我的应用程序的入职/演练。一切正常,除了当我导航到第一页时,navigationController 消失了。 当我关闭应用程序并再次启动它时,那里有 NavigationController/bar。 我的代码有问题吗?
@IBAction func buttonPressed(_ sender: Any) {
let storyborad = UIStoryboard(name: "Main", bundle: nil)
let mainVC = storyborad.instantiateViewController(withIdentifier: "mainVC") as! ViewController
self.present(mainVC, animated: true, completion: nil)
}
最佳答案
您需要为您的 VC 提供一个 UINavigationController
,或者将一个新的 VC 推送到当前的 navigationController
。
将 mainVC 推送到当前 navigationController
的第一种方法(在您的情况下可能会更好):
@IBAction func buttonPressed(_ sender: Any) {
let storyborad = UIStoryboard(name: "Main", bundle: nil)
let mainVC = storyborad.instantiateViewController(withIdentifier: "mainVC") as! ViewController
self.navigationController?.pushViewController(mainVC, animated: true)
}
第二种方法,初始化一个导航 Controller :
@IBAction func buttonPressed(_ sender: Any) {
let storyborad = UIStoryboard(name: "Main", bundle: nil)
let mainVC = storyborad.instantiateViewController(withIdentifier: "mainVC") as! ViewController
self.present(UINavigationController(rootViewController: mainVC), animated: true, completion: nil)
}
关于swift - 使用 NavigationController 转到另一个 Storyboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54082875/