swift - 使用 NavigationController 转到另一个 Storyboard

标签 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

最佳答案

您需要为您的 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/

相关文章:

ios - 模拟器不支持LAContext评估访问控制吗?

ios - 使用导航栏和 UITextField 时出现问题

iphone - UIViewController 和 UINavigationController 的问题

iOS:为什么没有显示导航栏?

ios - 滑动菜单 IOS 使用 aryaxt/iOS-Slide-Menu

ios - 如何在 UIScrollView 中处理 UIScrollView

ios - 使用取消和保存按钮操作将 Swift DatePicker 显示到 UIAlertController

arrays - "Value of type ' AnyObject ' has no member."..尝试在 Swift 中为数组中的对象赋值时发生错误

ios - 如何在 UINavigationController 中获得弹出 View 的通知?

ios - 水平移动 backButtonImage