Search code examples
iosswiftuiviewcontrolleruinavigationcontrolleruitabbar

How to check whether a specific UiViewcontroller(Tabbar Embedded) is in the navigationController rack?


I have a SignInVc as a starting point and a skip button. If user taps skip, e goes to home page and when he taps any button he is pushed to SignInVc.

The homeVC has TabBar, like its one of the 4 tab bar Vc's.

                   if let viewControllers = self.navigationController?.viewControllers {
                        for controller in viewControllers
                        {
                            if controller == (tabBarController?.viewControllers![0]){
                                print("FOUND IT")
                            }
                            print(controller)
                        }
                    }

While debugging with breakpoints, i can see the home page in navigationController?.viewControllers

But i cannot access it!!, the print is not executed. What should i use in the RHS of == ?

The plan is to push to the homeVC instead of print code.

EDIT:

I'm adding the screen shots of the debug below

Here i want to get to the view controller at Index 2

tabbar StoryBoard

Home StoryBoard

[Debug window][1]

Debug


Solution

  • I am not sure whether am right,

    logically declare a global variable,

    var initiateHomePage: Bool? // declare above or outside any swift file

    For button actions

    case 1

      @IBAction func skipButtonTapped(_ sender: Any) {
          initiateHomePage = true // should go homepage
        
        }
    

    case 2

      @IBAction func anyButtonTapped(_ sender: Any) {
          initiateHomePage = false // should go SignInVC
        
        }
    

    Atlast

    while executing

      if initiateHomePage == true {
          // redirect to home page
          // use this to redirect to tab bar 
           if let viewControllers = self.navigationController?.viewControllers {
                                for controller in viewControllers
                                {
                                    if controller == (tabBarController?.viewControllers![0]){
                                        print("FOUND IT")
                                    }
                                    print(controller)
                                }
                            } 
    
        //  or use this 
        let ViewController:UIStoryboard = UIStoryboard(name: "Module", bundle: nil)
            let tabBarController = ViewController.instantiateViewController(withIdentifier: "tabBar") as! UITabBarController
            let appDelegate = UIApplication.shared.delegate as! AppDelegate
            appDelegate.window?.rootViewController = tabBarController
         
        } else {
          // redirect to SignINVC 
         
        }