4

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

hanumanDev
  • 6,592
  • 11
  • 82
  • 146
  • 1
    http://stackoverflow.com/questions/3027559/can-i-pop-to-specific-viewcontroller – Arun Nov 14 '13 at 12:17
  • https://www.google.co.in/?gws_rd=cr&ei=ub6EUri8MMWVrgeymoGwCg#q=pop+to+view+controller – Arun Nov 14 '13 at 12:18
  • @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 Nov 14 '13 at 12:38

6 Answers6

3

Try this

    - (IBAction)back{
if(self.navigationController.viewControllers.count>2)
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-3] animated:YES];
}
Anand Suthar
  • 3,678
  • 2
  • 30
  • 52
2

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

}
Arun
  • 3,406
  • 4
  • 30
  • 55
1

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.:)

iSmita
  • 1,292
  • 1
  • 9
  • 24
0

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.

alex
  • 2,121
  • 1
  • 18
  • 24
0

You should try

NSArray *vcArray = [[self navigationController] viewControllers];
[self navigationController popToViewController:[vcArray objectAtIndex:vcArray.count-3] animated:YES];
Mert Buran
  • 2,989
  • 2
  • 22
  • 34
0

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];
        }
    }
}
IKKA
  • 6,297
  • 7
  • 50
  • 88