How to control multiple navigation controller in an iOS project

对着背影说爱祢 提交于 2021-02-08 06:39:20

问题


The structure of my project is as follows.

Initially when the user is registering or trying to login the initial navigation controller should work and after successfully registering / loggin in the user should be taken to first tab of tab bar controller. But the issue that i am facing is that i am getting 2 navigation bars in the tab bar view. Can someone guide me how to implement this in the correct way.

Thanks in advance


回答1:


Make one of the two navigation controllers from tabbar to initial viewcontroller and add the following in Appdelegate's didFinishLAunching

    if (isDashboardVC == nil || isDashboardVC == false)

         {
           let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
           let navigationController:UINavigationController = mainStoryboardIpad.instantiateViewController(withIdentifier: "navigationmain") as! UINavigationController
           self.window = UIWindow(frame: UIScreen.main.bounds)
           self.window?.rootViewController = navigationController
           self.window?.makeKeyAndVisible()
         }
     else
         {

            let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let tabBarIntial : UITabBarController = mainStoryboardIpad.instantiateViewController(withIdentifier: "Tabbarcontroller") as! UITabBarController

            let navigationController2:UINavigationController = mainStoryboardIpad.instantiateViewController(withIdentifier: "yourViewControllerName") as! UINavigationController
            let navigationController:UINavigationController = mainStoryboardIpad.instantiateViewController(withIdentifier: "yourViewControllerName") as! UINavigationController
            let navigationController3:UINavigationController = mainStoryboardIpad.instantiateViewController(withIdentifier: "yourViewControllerName") as! UINavigationController
            tabBarIntial.viewControllers = [navigationController2, navigationController, navigationController3]
            tabBarIntial.selectedIndex = 1
         }



回答2:


At some point, I would assume that the app is able to determine whether the user loggedin or not, based on that you have to set the desired root view controller for the app.

For such a case, the best place to do that is application(_:didFinishLaunchingWithOptions:) method in the AppDelegate file:

Tells the delegate that the launch process is almost done and the app is almost ready to run.

For simplicity, let's say that you are saving isLoggedin boolean in the UserDefault, so it could be achieved like this:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // the flag for determining whether the user loggedin or not
    let isLoggedin = UserDefaults.standard.bool(forKey: "K_isLoggedin")

    // the desired initial view controller (based on the value of `isLoggedin`)
    let initialViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: isLoggedin ? "TabbarIdentifier" : "FirstNavigationIdentifier")

    // setting the app rootViewController
    window?.rootViewController = initialViewController

    return true
}

Note that "TabbarIdentifier" represents the tabbar controller at the storyboard and also "FirstNavigationIdentifier" represents the first navigation view controller at the storyboard.

if you are unaware of how to set the view controller identifier, checking this answer should help.

Technically speaking, setting the desired root view controller means setting the rootViewController to the main window of the app (AppDelegate window).



来源:https://stackoverflow.com/questions/49913847/how-to-control-multiple-navigation-controller-in-an-ios-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!