0

I have a problem that my iOS app has several ViewControllers.

For example: ViewControllers named like A, B, C. A jumped to B with pushViewController, B jumped to C with presentViewController , C jumped to D with presentViewController and so on.

If the current ViewController is Z or some other ViewController, how can I jump back to A directly?

Binarian
  • 12,296
  • 8
  • 53
  • 84
Tanddd
  • 47
  • 10

4 Answers4

1

Try Below code. It will work for all cases :

for (UIViewController *controller in self.navigationController.viewControllers)
{
        if ([controller isKindOfClass:[nameOfYourViewControllerYouWantToNavigate class]])
        {
            if(controller.isBeingPresented)
                 [controller.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
            else
                 [self.navigationController popToViewController:controller animated:YES];

            break;
        }
}
Ekta Padaliya
  • 5,743
  • 3
  • 39
  • 51
  • Thanks , let me have a try~ – Tanddd Jun 30 '16 at 07:59
  • @Tanddd If the answer is working then please accept it. This is motivation for us. – Ekta Padaliya Jul 05 '16 at 06:30
  • Sorry for forgeting to accept.In the situation described before, the code won't work because of too much 'presentViewController' and 'pushViewController'. Finally, I've resolved it by adding a return function to Base VC. Thanks for your help~~ – Tanddd Jul 05 '16 at 11:29
  • Hard to read my words, isn't it ? Thanks to everyones patience, I think maybe I haven't describe my problem clearly because of my poor english, but there are still so many people who give me advice. Also, I wish I could share my solution, but my english is really bad that I might not describe it clearly. Haha...I will update the code in the future. – Tanddd Jul 05 '16 at 12:14
1
[[self navigationController] popToRootViewControllerAnimated:YES];

It will pop your viewcontroller to Root controller.

Raj Aggrawal
  • 761
  • 1
  • 6
  • 21
0

For the view controllers show by pushViewController, you can get rootViewController via [self.navigationController.viewControllers objectAtIndex:0]

Others shown by presentViewController, you can get parent view controller via self.presentingViewController

J.Hunter
  • 576
  • 4
  • 14
0

Whatever you want to go back to A ViewController from any other view controller, first you have to set that as RootViewController. If you use XIB you must set as root in app delegate didFinishLaunchWithOptions method. If you use storyboard you should set NavigationController in storyboard and set AViewController as ROOT using control+drag(mouse). I work in a project which has lot of view controllers. It has pushViewController and PresentViewControllers also I set A asRootViewController. So I can return from any view controller to RootViewController.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
      // Override point for customization after application launch.
     self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
     AViewController *aVC = [[AViewController alloc]initWithNibName:@“AViewController" bundle:nil];
     UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:aVC];
     self.window.rootViewController = aVC;
     [navController setNavigationBarHidden:YES];
     self.window.backgroundColor = [UIColor clearColor];
     [self.window makeKeyAndVisible];
     return YES;
}

For going to AViewController from any other view controller

- (IBAction)actionGoBack:(id)sender 
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
user3182143
  • 9,459
  • 3
  • 32
  • 39