0
votes

I have a navigation controller with three views. Possible navigation routes are:

VC1 -> VC3 and
VC1 -> VC2 -> VC3

When a user goes from VC1 to VC2 and ends up in VC3, I would like the back button in the navigation bar to point to VC1 (and not VC2). So in VC2, I added this piece of code.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    self.navigationController?.popViewController(animated: true)
}

It works; but I can see the pop happening. When I am on VC2 and in the process of navigating to VC3, I can see VC1 for a second (VC2 has been popped from the stack) and then I see VC3. It doesn't look nice.

Is there a better way of doing this?

Thanks.

6
Unwind segue is all you need. Read stackoverflow.com/questions/44513291/… - Sandeep Bhandari
try this : self.navigationController?.popToRootViewController(animated: true) this code use to you popback vc3 to direct vc1. - Dixit Akabari
@Dix Please read the answers bevor you post a comment. - dasdom
I have updated my answer please check if you want i have demo ready too - Varun Naharia

6 Answers

3
votes

Use popToRootViewControllerAnimated:. If I understand your problem correctly, this is exactly what you need.

1
votes

you can use this code, when back button pressed on VC3. For this you need to add action to back button of VC3.

self.navigationController?.popToRootViewController(animated: true)
0
votes

I have used below code to pop views from stack of viewcontrollers.

var allViewControllers = navigationController?.viewControllers

allViewControllers?.removeLast(2) //number of views to pop

navigationController?.setViewControllers(viewControllers!, animated: true)

This will not give you that view animation problem. Hope this helps. Happy Coding.

0
votes

After navigating to the VC3, remove VC2 from the navigation stack of the navigation controllers. Below is the code for this, just write your view controller's index that is 2 in your case or use it dynamically to work.

self.navigationController?.viewControllers.removeAtIndex("insert here a number")

0
votes

Use popToRootViewController like what the others are saying

But if VC1 is not your rootViewController, add this code to VC3 when popping

for vc in (self.navigationController?.viewControllers ?? []) {
  if vc is VC1 {
    _ = self.navigationController?.popToViewController(vc, animated: true)
    break
  }
}
0
votes

You can use below extension to pop to any viewController directly

extension UINavigationController {

    func popTo(controllerToPop:UIViewController)  {
        var controllersArray = self.viewControllers
        var index = 0
        var isFound = false
        for vc in controllersArray {
            if(vc.isKind(of: controllerToPop.classForCoder))
            {
                isFound = true
                break
            }
            index += 1
        }
        if(isFound)
        {
            for i in index+1 ..< controllersArray.count-1
            {
                controllersArray.remove(at: i)
            }
            self.viewControllers = controllersArray
        }
        self.popViewController(animated: true)
    }
}