(Swift) popping back to navigation controller

395 Views Asked by At

I will describe what I want to do. I have 3 viewControllers. The first one should be navigation one, and I think I have made mistake there in code.
First VC leads to Second, and the second VC leads to Third VC.The third one has got a button which should lead back to the first one.

This is how I present secondVC from the firstVC:

let mainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let secondVC = (mainStoryboard.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController)   
presentVC(SecondVC)

func presentVC(_ VC: UIViewController) {
        let navController = UINavigationController(rootViewController: VC)
        navController.modalPresentationStyle = .fullScreen
        self.navigationController?.present(navController, animated: true, completion: nil)
    }

Now in the secondVC, when I click Close in right navBarItem, the thirdVC should be opened and it works fine, here is the code:

In ViewDidLoad :

 self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Close", style: .plain, target: self, action:
#selector(closeSecondVC))

And after:

  @objc
    func closeSecondVC() {
    let mainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
            let thirdVC = (mainStoryboard.instantiateViewController(withIdentifier: "ThirdViewController") as! ThirdViewController)
            presentVC(thirdVC) //Same function as above.
}

And on this buttonClick in the thirdVC, I need to return to firstVC, that's where I am lost:

 @IBAction func btnTapped(_ sender: Any) {
        if let navController = self.navigationController {
            navController.popViewController(animated: true)
        }  //nothing happens on click
    }
4

There are 4 best solutions below

3
Daljeet On BEST ANSWER

Use below code in btnTapped:

    for controller in self.navigationController!.viewControllers as Array 
    {
      // here YourViewController is your firstVC      
     if controller.isKind(of: YourViewController.self) {
            self.navigationController!.popToViewController(controller, animated: true)
            break
        }
    }

And don't present your view push your view to pop.

3
Ketan Parmar On

When you're presenting any view controller you need to dismiss it instead of pop! When you're pushing any view controller at that time you can pop but here you're presenting view controller in this case you should dismiss it like below,

dismiss(animated: true) {

    }

or you should push new view controller and then you can pop it!

0
Vasucd On

You are presenting navigation controller , navController.popViewController() will not work.

Try to push navigation using

self.navigationController?.pushViewController(vc, animated: true)

Now if you want to go from ViewController3 to ViewController1 try

for controller in self.navigationController!.viewControllers as Array {
    if !controller.isKind(of: ViewController1.self) {
        self.navigationController!.popToViewController(controller, animated: true)
    } 
 else{
     break;
  }
}
0
Munzareen Atique On

You need to call this to get back to the first ViewController :

self.navigationController?.popToRootViewController(animated: true)