4
votes

How would I be able to jump back two steps in a view controller navigation and still preserve the navigation?

I have vc1 which on a touch moves to vc2 and then to vc3. how would I jump to vc1 from vc3 for example?

thanks

6
@hanumanDev these hints not helps you ... ? then explain your problem more or if you found any better way to do this then post here it will help others.Anand Suthar

6 Answers

3
votes

Try this

    - (IBAction)back{
if(self.navigationController.viewControllers.count>2)
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-3] animated:YES];
}
2
votes

Just a copy but safer approach... try like this

- (void) turnBackToAnOldViewController{

    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;
        }
    }

}
1
votes

You can use this -

NSArray *controllers = [self.navigationController viewControllers];
NSLog(@"%@",controllers);

Here you get array of controllers.

int count = [controllers count];

Just pass index and switch wherever you want.

UIViewController *theControllerYouWant = [self.navigationController.viewControllers objectAtIndex:(count - 2)];
[self.navigationController popToViewController:theControllerYouWant animated:NO];

Hope this helps you.Thank you.:)

0
votes

Or, more flexible solution - work with navigation stack directly.

Ex.

 NSArray *viewControllers = self.navigationController.viewControllers;
 self.navigationController.topViewController = viewControllers[i];

where i can be any vc from available in stack controllers.

0
votes

You should try

NSArray *vcArray = [[self navigationController] viewControllers];
[self navigationController popToViewController:[vcArray objectAtIndex:vcArray.count-3] animated:YES];
0
votes

Please try like this

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY SELF.class.description == %@“,[[VC1 class]description]];
BOOL exist = [predicate evaluateWithObject:[self.navigationController viewControllers]];
if (exist) {

    for (id object in [self.navigationController viewControllers]) {

        if ([object isKindOfClass:[VC1 class]]) {

            [self.navigationController popToViewController:object animated:YES];
        }
    }
}