1
votes

I have 3 VC's (VC as in ViewController)

1 AuthViewController which is the entry point for the app and it checks if user is logged in else it displays the sign-in related views

2 HomeViewController is the main interface of the app landing page after the user has logged in

3 ListViewController which displays a list, invoked via a segue on HomeViewController.

I want to enable back navigation between ListViewController and HomeViewController via a Navigation Controller.

How can I achieve this If I have a Navigation controller whose root VC is HomeVC. how can I invoke it from my AuthVC so that it gets invoked with the Navigation controller.

I have tried to invoke the Navigation controller but did not work

I have also tried invoking the HomeVC by

 let mainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
     if let viewController = mainStoryboard.instantiateViewController(withIdentifier: "mainViewController") as? UIViewController {
        viewController.modalPresentationStyle = .overCurrentContext
         self.present(viewController, animated: true, completion: nil)

although I was able to invoke the HomeVC could not get the navigation buttons

How can I invoke HomeVC from the AuthVC without loosing the NavigationController

1

1 Answers

2
votes
  1. You need 1 navigation controller, not 3.
  2. If you want to push a new controller, then use push method, not present.
  3. The back button gets enabled automatically on the navigation bar if there are controllers in the navigation stack and the current controller is not a root view controller.
  4. When coming from AuthViewController to HomeController, you should change the RootViewController. So that the user cant go back to the Auth screen. That would make no sense.
  5. Once you are on HomeController, you can push other controller using the below code and it will add a back button to your controller automatically.

    let mainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
    if let viewController = mainStoryboard.instantiateViewController(withIdentifier: "mainViewController") as? UIViewController {
       self.navigationController?.pushViewController(vc, animated: true)
    }
    

You can also use the below extension:

extension UIViewController {

    func pushVC(storyboardName : String, vcname : String)  {
        let vc = UIStoryboard.init(name: storyboardName, bundle: Bundle.main).instantiateViewController(withIdentifier: vcname)
        vc.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(vc, animated: true)
    }

    func popVC() {
        self.navigationController?.popViewController(animated: true)
    }

    func makeRootVC(storyBoardName : String, vcName : String) {

        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let vc = UIStoryboard(name: storyBoardName, bundle: Bundle.main).instantiateViewController(withIdentifier: vcName)
        let nav = UINavigationController(rootViewController: vc)
        nav.navigationBar.isHidden = true
        appDelegate.window?.rootViewController = nav // If using XCode 11 and above, copy var window : UIWindow? in your appDelegate file
        let options: UIView.AnimationOptions = .transitionCrossDissolve
        let duration: TimeInterval = 0.6
        UIView.transition(with: appDelegate.window!, duration: duration, options: options, animations: {}, completion: nil)
    }
}