iOS 13 使用导航控制器从视图控制器移动到另一个故事板

iOS 13 move from view controller to another storyboard with its navigation controller

我有两个故事板

  1. Main.storyboard
  2. App.storyboard

当用户登录时,它从 loginViewController 移动到 ChatVC 我想使用 ChatVC 的嵌入式导航控制器。我写了下面的代码但在这种情况下创建了自己的新导航控制器。有什么方法可以使用 App.storyboard 的导航控制器吗?

      guard let rootVC = UIStoryboard.init(name: "App", bundle: nil).instantiateViewController
      (identifier: "MessaageVC") as? MessaageVC else {
                return
            }
            let navigationController = UINavigationController(rootViewController: rootVC)
            UIApplication.shared.windows.first?.rootViewController = navigationController
            UIApplication.shared.windows.first?.makeKeyAndVisible()

如果您有自定义 UINavigationController class,则应将 as? UINavigationContoller 替换为您的 class。您可以根据需要更改故事板名称。

  if let nav = UIStoryboard(name: "App", bundle: nil).instantiateViewController(withIdentifier: "yourNavigationControllerIdentifier") as? UINavigationController{
            let rootVc  = UIStoryboard(name: "App", bundle: nil).instantiateViewController(withIdentifier: "yourRootVCIdentifier")
            nav.viewControllers = [rootVc]
            UIApplication.shared.keyWindow?.rootViewController = nav
        }

您必须将 navigation controller 设置为 UIApplication

rootViewController
 guard let rootVC = UIStoryboard.init(name: "App", bundle: nil).instantiateViewController
      (identifier: "MessaageVC") as? MessaageVC else {
                return
            }
            let navigationController = UINavigationController(rootViewController: rootVC)
            UIApplication.shared.keyWindow?.rootViewController = navigationController
            UIApplication.shared.keyWindow?.makeKeyAndVisible()