简体   繁体   中英

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

I have two storyboards

  1. Main.storyboard
  2. App.storyboard

when user login it move from loginViewController to ChatVC i want to use ChatVC's embedded navigation controller. I written the code below But in this case in create its own new navigation controller. Is there any way to use navigation controller of 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()

If you have custom UINavigationController class then you should replace as? UINavigationContoller as? UINavigationContoller with your class. You can change storyboard name according to your need.

  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
        }

You have to set your navigation controller as rootViewController of UIApplication

 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM