使用 NavigationController 转到另一个 Storyboard

Segue to another Storyboard with NavigationController

我正在尝试为我的应用修复 onboarding/walkthrough。一切正常,除了当我导航到第一页时,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)

}

您需要用 UINavigationController 展示您的 VC,或者将新的 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)
}

第二种方法,初始化导航控制器:

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