1

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
    }
faris97
  • 332
  • 2
  • 17

4 Answers4

2

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.

Daljeet
  • 1,454
  • 2
  • 16
  • 38
  • It doesn't work. I should maybe use push everywhere but I can not get rid of left navBarItem which comes with pushing. – faris97 Feb 12 '20 at 12:14
  • To pop out you have to push and you can hide navBarItem on push which is very simple. – Daljeet Feb 12 '20 at 12:15
  • Ok I will try to use push from firstVC to second VC and also push from secondVC to thirdVC. Will I then be able to pop from 3rd to the 1sr vc? – faris97 Feb 12 '20 at 12:17
1

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!

Ketan Parmar
  • 25,426
  • 9
  • 43
  • 67
  • If I dismiss the thirdVC it will let me back to the secondVC. I want to go back to the firstVC. – faris97 Feb 12 '20 at 12:09
  • then push a new view controller and call `self.navigationController?.popToRootViewController(animated: true)` to go to initial view controller! and do not do `let navController = UINavigationController(rootViewController: VC)` every time! You should embed navigation controller to rootview controller once! – Ketan Parmar Feb 12 '20 at 12:11
  • You should google `Navigation controller tutorials`, you'll find many tutorials. for example,https://medium.com/whoknows-swift/swift-the-hierarchy-of-uinavigationcontroller-programmatically-91631990f495 – Ketan Parmar Feb 12 '20 at 12:18
0

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;
  }
}
Vasucd
  • 319
  • 2
  • 9
0

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

self.navigationController?.popToRootViewController(animated: true)