I have 4 view controllers (A,B,C,D) and they are connected in this order, and a navigation controller pointed to A. My question is is there a way for me to jump from A to C directly?

- 5,862
- 10
- 58
- 91

- 2,632
- 7
- 29
- 45
-
2What do you mean by "connected"? Is this a storyboard application? Do you want to load B on the way to C? Need some more info here. – Matt S. Apr 03 '14 at 03:14
-
It depends how you have come back to A.If you have popped then you have to alloc init viewcontroller and push to navigation stack else you can iterate over navigationcontoller.controllers array and pop till specific viewcontroller – amar Apr 03 '14 at 03:17
8 Answers
You can create a UIStoryboardSegue
that connects A to C, then call performSegueWithIdentifier:
with the corresponding ID in the segue to trigger it.

- 18,369
- 7
- 84
- 116
-
I found the implementation here: http://stackoverflow.com/questions/18550594/how-do-i-use-performseguewithidentifier-sender anyway, I appreciate your simple answer. – photosynthesis Apr 03 '14 at 03:55
For Swift 3
let viewControllers: [UIViewController] = self.navigationController!.viewControllers ;
for aViewController in viewControllers {
if(aViewController is YourViewController){
self.navigationController!.popToViewController(aViewController, animated: true);
}
}

- 5,414
- 3
- 39
- 45
Yes, all you need to do is instantiate the view controller, then pushViewController:animated:
UINavigationController should do the rest for you, including the popping when you're ready.
hope that helps, here is the source

- 3,786
- 3
- 26
- 47
If you are using nib then this code will help
NSArray *viewControllersArray=[self.navigationController viewControllers];
id viewController ;
UIViewController *homeVC=nil;
for(int i=0;i<[viewControllersArray count];i++)
{viewController = [viewControllersArray objectAtIndex:i];
if([viewController isKindOfClass:[classname class]])
{ homeVC=[viewControllersArray objectAtIndex:i];
break;
}
}if(homeVC)
{
[self.navigationController popToViewController:homeVC animated:YES];
}else{
classname *objViewController=[[classname alloc]init];
[self.navigationController pushViewController:objViewController animated:YES];
[objViewController release];
}

- 4,285
- 8
- 40
- 52
SWIFT
if you want to go back to a particular view controller
for viewcontroller in self.navigationController!.viewControllers as Array {
if viewcontroller.isKindOfClass(HomeVC) { // change HomeVC to your viewcontroller in which you want to back.
self.navigationController?.popToViewController(viewcontroller as! UIViewController, animated: true)
break
}
}

- 1
- 1

- 17,485
- 5
- 50
- 66
Go like this to a particular view controller
[self.navigationController popToViewController:[[self.navigationController viewControllers]objectAtIndex:1] animated:YES];

- 331,213
- 40
- 305
- 339

- 1
Yes You can. For safety, let's assume that you don't know, the order in which your View Controllers are connected. So, now, you just need a class assigned to your Controller, C. Let's Say, it is ViewController_C.h.
Step 1: Go to the class inspector in storyboard and assign this ViewController_C class to the controller, C.
Step 2: Now in the View Controller class which is assigned to your controller, A, write the following method.
-(int)getViewControllerIndex{
int i=0;
for(UIViewController *vc in [self.navigationController viewControllers]){
if([vc isKindOfClass:[ViewController_C class]]){
return i;
}
i++;
}
return 0;
}
and then call this method whenever you want to jump to Controller, C.
int index = [self getViewControllerIndex];
[self.navigationController popToViewController:[[self.navigationController viewControllers]objectAtIndex:index] animated:YES];

- 6,651
- 3
- 36
- 58