Kenpachi
Kenpachi

Reputation: 107

Redirect after login to a tab bar controller

in a project, I have a login system. If the login is accepted I put this piece of code to redirect to the first page.

  let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default){ action in

                        if let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DashboardViewController") as? DashboardViewController{

                            if let navigator = self.navigationController {
                                navigator.pushViewController(viewController, animated: true)
                            }
                        }


                    }



                    myAlert.addAction(okAction);
                    OperationQueue.main.addOperation {
                    self.present(myAlert, animated: true, completion: nil)
                    }

everything works but now on my first page "DashboardViewController" I have added one. tab bar controller.

enter image description here

if I redirect to page 1 the tab bar controller is not taken into account but if I start my project by tab bar controller everything works. so my question is how to redirect to tab bar controller after my login

Upvotes: 1

Views: 2363

Answers (3)

Shane Shelton
Shane Shelton

Reputation: 37

I handled it slightly differently.

 func transitionToHome() {

    let tabBarViewController = UIStoryboard(name: Constants.Storyboard.mainStoryBoard, bundle: nil).instantiateViewController(withIdentifier: Constants.Storyboard.tabBarController) as! UITabBarController

      view.window?.rootViewController = tabBarViewController
      view.window?.makeKeyAndVisible()

}

I am reassigning the tabBarController to be the root view so that the user can return without having to log in again. I also use a constant file to keep my constants organized for complex projects. You can see how I reference them in the code above.

struct Constants {

struct Storyboard {

    static let homeViewController = "HomeVC"

    static let tabBarController = "HomeTVC"

    static let mainStoryBoard = "Main"
}

}

Upvotes: 0

Nishant Bhindi
Nishant Bhindi

Reputation: 2252

let tabBarVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "TabBarVC") as! TabBarVC
    tabBarVC.selectedIndex = //your selected tab index
    navigationController?.pushViewController(tabBarVC, animated: true)

Dont redirect dashboardVC go with tabbarVC and then select tabIndex that you want your first page

Upvotes: 2

Aravind A R
Aravind A R

Reputation: 2714

You can redirect to tabbarController like

    let tabBarController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: tabBarControllerIdentifier)
    if let navigator = self.navigationController {
         navigator.pushViewController(tabBarController, animated: true)
}

Upvotes: 1

Related Questions