使用NavigationController搜索到另一个Storyboard

时间:2019-01-07 22:39:10

标签: swift uinavigationcontroller uinavigationbar

我正在尝试为我的应用修复入门/演练。一切正常,但当我导航到第一页时,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)

}

enter image description here

1 个答案:

答案 0 :(得分:1)

您需要向您的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)
}

第二种方法,显示初始化的NavigationController:

@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)
}