2

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?

klcjr89
  • 5,862
  • 10
  • 58
  • 91
photosynthesis
  • 2,632
  • 7
  • 29
  • 45
  • 2
    What 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 Answers8

2

You can create a UIStoryboardSegue that connects A to C, then call performSegueWithIdentifier: with the corresponding ID in the segue to trigger it.

Wayne Hartman
  • 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
2

try Unwind segue

http://chrisrisner.com/Unwinding-with-iOS-and-Storyboards

What are Unwind segues for and how do you use them?

Community
  • 1
  • 1
Pham Hoan
  • 2,107
  • 2
  • 20
  • 34
2

For Swift 3

    let viewControllers: [UIViewController] = self.navigationController!.viewControllers ;
    for aViewController in viewControllers {
        if(aViewController is YourViewController){
           self.navigationController!.popToViewController(aViewController, animated: true);
        }
    }
Museer Ahamad Ansari
  • 5,414
  • 3
  • 39
  • 45
1

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

MrHaze
  • 3,786
  • 3
  • 26
  • 47
1

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];
}
amar
  • 4,285
  • 8
  • 40
  • 52
1

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
            }
        }
Community
  • 1
  • 1
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
0

Go like this to a particular view controller

[self.navigationController popToViewController:[[self.navigationController viewControllers]objectAtIndex:1] animated:YES];
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
shyju
  • 1
0

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.

enter image description here

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];
Natasha
  • 6,651
  • 3
  • 36
  • 58