23

I have been trying to pop my view controller to one of previous view controllers in my view stack. Assume that, There are firstVC, secondVC, thirdVC and fourthVC viewcontrollers in my view stack. The current view controller is fourth one, and there is a tableview in fourthVC. If user delete all the rows in tableview, I should direct the user to secondVC. I had an idea that I would create another navigationcontroller and present it with presentViewController command. However, this is not a solution for my problem. Because I thougt that a navigation problem appears for this case. How can I find best solution for this case ?

Thank you for your answers, Best regards

serhanaksut
  • 634
  • 3
  • 8
  • 25

4 Answers4

35

Instead of doing a generic popViewControllerAnimated: call, use popToViewController:animated:. You could detect if the user has deleted all of the rows in which case, do something like this (otherwise just pop one view controller):

let viewControllers: [UIViewController] = self.navigationController!.viewControllers as [UIViewController];
self.navigationController!.popToViewController(viewControllers[viewControllers.count - 2], animated: true);
Aaron Wojnowski
  • 6,352
  • 5
  • 29
  • 46
  • I created an UIViewController extension who is more safer, I also created a second extension to return to a specific viewcontroller. https://stackoverflow.com/a/44626461/2370587 – Maximelc Jun 19 '17 at 09:12
19

If you want to pop to a specific view control and dont know the count to go back you can use this:

 let viewControllers: [UIViewController] = self.navigationController!.viewControllers as! [UIViewController];

        for aViewController in viewControllers {
            if(aViewController is ViewControllerYouWantToGoTo){
                 self.navigationController!.popToViewController(aViewController, animated: true);
            }
        }
BluGeni
  • 3,378
  • 8
  • 36
  • 64
12

After lots of effort I have created swift extension of back to a particular view controller in Swift 3.0.

extension UINavigationController {

    func backToViewController(viewController: Swift.AnyClass) {

            for element in viewControllers as Array {
                if element.isKind(of: viewController) {
                    self.popToViewController(element, animated: true)
                break
            }
        }
    }
}

Method calling:

   self.navigationController?.backToViewController(viewController: BarCodeScannerVC.self)
Arvind Kumar
  • 2,371
  • 1
  • 18
  • 25
0

Here you can do this by the find the controller from the UINavigationController stack and with the help of for loop and check the condition of your desire controller and if the condition gets fulfilled it will pop to the destionation controller.

let viewControllersStack: [UIViewController] = self.navigationController!.viewControllers
      for firstViewcontroller in viewControllersStack
      {
           if firstViewcontroller is desireViewController
           {
                self.navigationController!.popToViewController(firstViewcontroller, animated: true)
           }
      }
Azharhussain Shaikh
  • 1,654
  • 14
  • 17