Ankit Vyas
Ankit Vyas

Reputation: 7501

Can i pop to Specific ViewController?

I am using navigation based application. I push First ViewController to Second ViewController and from Second ViewController to Third ViewController. Now I want to pop from Third ViewController to First ViewController.I am performing this task using the below code but my application crashed.

Please any body give me some proper guidelines. I can't use pop to rootViewController because it's different viewController. Thanks in advance...

In Third ViewControler i have written this:

FirstViewCtr *x=[[FirstViewCtr alloc] initWithNibName:@"FirstViewCtr" bundle:nil];
[self.navigationController popToViewController:x animated:NO];

Upvotes: 54

Views: 56795

Answers (16)

Ashu
Ashu

Reputation: 3523

i have answer here. This is 100% working code for Swift > 4.X

How can I pop specific View Controller in Swift

Upvotes: 0

Umair
Umair

Reputation: 1253

Swift 4 version

if let viewController = navigationController?.viewControllers.first(where: {$0 is YourViewController}) {
      navigationController?.popToViewController(viewController, animated: false)
}

You may specify another filter on .viewControllers.first as per your need e.g lets say if you have same kind of view controllers residing in the navigation controller then you may specify an additional check like below

  if let viewController = navigationController?.viewControllers.first(where: {
        if let current = $0 as? YourViewController {
             return current.someProperty == "SOME VALUE"
        }
       return false } ) {
            navigationController?.popToViewController(viewController, animated: false)
   }

Upvotes: 10

Harsh Pipaliya
Harsh Pipaliya

Reputation: 2431

Put function in UIViewController 1. it checks if Specific UIViewController exists In UINavigationController then popToViewController or else pushViewController

func navigate(_ navVC: AnyClass, pushVC: UIViewController) {
    for obj in self.navigationController!.viewControllers {
        if obj.isMember(of: navVC) {
            self.navigationController!.popToViewController(obj, animated: true)
            return
        }
    }
    self.navigationController!.pushViewController(pushVC, animated: true)
}

Use

self.navigate(ViewController.self, pushVC: self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController)

Upvotes: 0

Ankit Vyas
Ankit Vyas

Reputation: 7501

for controller in self.navigationController!.viewControllers as Array {
        if controller.isKind(of: LoginVC.self) {
            _ =  self.navigationController!.popToViewController(controller, animated: true)
            break
        }
    }

Upvotes: 0

Yunus Nedim Mehel
Yunus Nedim Mehel

Reputation: 12389

A safer approach:

- (void)turnBackToAnOldViewController{

    for (UIViewController *controller in self.navigationController.viewControllers) {

        //Do not forget to import AnOldViewController.h
        if ([controller isKindOfClass:[AnOldViewController class]]) { 

            [self.navigationController popToViewController:controller
                                                  animated:YES];
            return;
        }
    }
}

Upvotes: 80

Kiran Jadhav
Kiran Jadhav

Reputation: 3327

Updated for Swift 3:

used below simple code, for pop to specific view controller;

 for vc in self.navigationController!.viewControllers as Array {
          if vc.isKind(of: YourViewControllerName) {
               self.navigationController!.popToViewController(vc, animated: true)
               break
          }
    }

Upvotes: 1

Bartosz Olszanowski
Bartosz Olszanowski

Reputation: 818

I think that .filter({...}).first is a little bit slower than .first(where: {...}). Also this could be written more precisely to address only UIViewControllers.

extension UINavigationController {
    func popToController<T: UIViewController>(_ type: T.Type, animated: Bool) {
        if let vc = viewControllers.first(where: { $0 is T }) {
            popToViewController(vc, animated: animated)
        }
    }
    func popToControllerOrToRootControllerIfNotInTheStack<T: UIViewController>(_ type: T.Type, animated: Bool) {
        if let vc = viewControllers.first(where: { $0 is T }) {
            popToViewController(vc, animated: animated)
        } else {
            popToRootViewController(animated: animated)
        }
    }
}

Upvotes: 1

Ketan P
Ketan P

Reputation: 4379

Implemented & Tested in Swift 3.0

Below is Method which can useful for Navigate to any specific View Controller :

func poptoSpecificVC(viewController : Swift.AnyClass){
        let viewControllers: [UIViewController] = self.navigationController!.viewControllers
        for aViewController in viewControllers {
            if aViewController.isKind(of: viewController) {
                self.navigationController!.popToViewController(aViewController, animated: true)
                break;
            }
        }
    }

Usage :

self.poptoSpecificVC(viewController: createIntervalVC.self)

Upvotes: 2

budiDino
budiDino

Reputation: 13557

Quick and safe Swift 3 version:

if let vc = navigationController.viewControllers.filter({ $0 is SpecificViewControllerClass }).first {
  navigationController.popToViewController(vc, animated: true)
}

Upvotes: 2

Ved Rauniyar
Ved Rauniyar

Reputation: 1589

After lots of effort someone has 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: YourViewController.self)

Upvotes: 1

Vikash Kumar
Vikash Kumar

Reputation: 636

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];

Upvotes: 4

SwiftGod
SwiftGod

Reputation: 396

Swifty way:

     let dashboardVC = navigationController!.viewControllers.filter { $0 is YourViewController }.first!
     navigationController!.popToViewController(dashboardVC, animated: true)

Upvotes: 20

Mahendra Y
Mahendra Y

Reputation: 1949

- (void) RetunToSpecificViewController{

   for (UIViewController *controller in self.navigationController.viewControllers)
    {
           if ([controller isKindOfClass:[AnOldViewController class]])
           {
            //Do not forget to import AnOldViewController.h

                       [self.navigationController popToViewController:controller
                                                             animated:YES];
                        break;
            }
    }
}

Upvotes: 6

Leszek Zarna
Leszek Zarna

Reputation: 3317

Often it is more important to do that from top of stack, so:

- (void)popToLast:(Class)aClass
{
    for (int i=self.navigationController.viewControllers.count-1; i>=0; i--)
    {
        UIViewController *vc = self.navigationController.viewControllers[i];
        if ([vc isKindOfClass:aClass])
        {
            [self.navigationController popToViewController:vc animated:YES];
            break;
        }
    }
}

and you call that

popToLast:[SomeViewController class];

Upvotes: 6

Ankit Vyas
Ankit Vyas

Reputation: 7501

By Writing the First Line you get the Indexes of all View Controllers and from second Line You will reach up to your Destination.

NSArray *array = [self.navigationController viewControllers];

[self.navigationController popToViewController:[array objectAtIndex:2] animated:YES];

Upvotes: 95

drawnonward
drawnonward

Reputation: 53689

Your code creates a new instance of a view that has never been pushed onto the stack, then tries to pop back to that controller.

If you are popping back to the root view controller, you can uses popToRootViewControllerAnimated:

If you are popping back a known distance you can call popViewControllerAnimated: more than once. In your example, that would be 2 controllers so to calls. You could do the same thing by looking in viewControllers for the controller 2 from the end and popping to it.

The above suggestions are quick fixes. One best practice scenario would be to pass the controller you want to return to along to each successive controller you push. First passes itself to second, second passes that reference to third, third pops to the passed reference, which is first.

In effect, you are creating a temporary root controller. You could subclass UINavigationController and add a temporaryRoot property and a popToTemporaryRootViewControllerAnimated: method that would pop to your temporary root and clear it. When first pushes seconds, it would also set itself as the temporary root so that every controller in the stack does not have to pass a reference around. You would have to add some extra checks to unsure you never pop past the temporaryRoot without clearing it.

Upvotes: 1

Related Questions