7

What I want to do seems pretty simple, but I can't find any answers on the web. I have the login screen as the root view controller and then tab bar controller and in every tab I have a navigation controller.

I have used storyboard and the hierarchy is described below,

Root VC
    |
     --- tabbar controller
          |
           ---Navigation Controller
                |
                 --- VC1

Requirement is to navigate back to the root view controller from VC1. How can we achieve this?

Vinay Jain
  • 2,644
  • 3
  • 26
  • 44
  • 2
    You should use an unwind segue to do this. – rdelmar Nov 20 '13 at 16:47
  • Thanks for your answer @rdelmar, I agreed to your solution. But the issue i am facing now is, i am unable to connect the button outlet to exit button of storyboard to unwind it. It was not accessible, it seems. Can you please help me on this. – Vinay Jain Nov 21 '13 at 05:02
  • Look at my answer here (http://stackoverflow.com/questions/16158586/app-running-slow-after-loading-viewcontroller-then-unload-about-15-20-times/16160239#16160239), and see if that's how you're trying to do it. – rdelmar Nov 21 '13 at 05:19

5 Answers5

3

I got the resolution to the issues using the "Unwind Segues".

Step 1) The bare minimum you need is to subclass the view controller for your destination view (aka, a view that has popped up previously in navigation and you want to unwind to it) and add a method like this to it (the method name can be anything you want, but it should be unique because all unwind segues in your entire app are listed together):

- (IBAction)unwindToViewControllerNameHere:(UIStoryboardSegue *)segue {
//nothing goes here
}

Step 2) Now, in your source view (aka, the view that you want to unwind from) you simply drag a segue from your button or whatever down to the little green "EXIT" icon at the bottom of your source view. There should now be an option to connect to "- unwindToViewControllerNameHere"

That's it, your segue will unwind when your button is tapped. And we can move to any view controller we want and rest of the view controllers will be released.

Vinay Jain
  • 2,644
  • 3
  • 26
  • 44
2

If you have controllers hierarchy like

--- Navigation Controller -- | Root VC | --- tabbar controller | --- Navigation Controller -- | --- VC1

and there is a UIButton on VC1, so on click of that you want to move to root viewcontroller (Root VC) then use :

-(void)moveToRootViewController {

    //Move to root viewController
    UINavigationController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"mainnav"];

    self.view.window.rootViewController = controller;

 }

here mainnav is storyboard identifier of root viewcontroller Navigation Controller.

ViewControllers hierarchy

According to picture white colour viewcontroller is a root viewcontroller and tabBarController have 2 tabs with navigation controller and if you want to move to root viewcontroller from second tab viewcontroller UIButton(black colour) click then use the above code.

If you have hierarchy like --- Navigation Controller -- | Root VC | --- VC1---- |--- VC2---- |

and want to move to root viewcontroller (Root VC) from VC1 or from VC2 then use :

[self.navigationController popToRootViewControllerAnimated:YES]; 
Nikhlesh Bagdiya
  • 4,316
  • 1
  • 19
  • 29
0

Use following code

- (void) forRootViewCon {
    UINavigationController *nav = (UINavigationController*) self.view.window.rootViewController;
    UIViewController *root = [nav.viewControllers objectAtIndex:0];
    [root performSelector:@selector(returnToRoot)];
}

Call method name is returnToRoot

- (void)returnToRoot {
    [self dismissViewControllerAnimated:NO completion:nil];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

OR

[self.navigationController popToRootViewControllerAnimated:YES];
iPatel
  • 46,010
  • 16
  • 115
  • 137
0

This should be enough.

[self.navigationController popToRootViewControllerAnimated:YES];
James Webster
  • 31,873
  • 11
  • 70
  • 114
0

You should enable the Root VC in the navigation controller :

--- Navigation Controller -- | Root VC | --- tabbar controller | --- VC1

And then call

[navController popToRootViewControllerAnimated:YES];

EDIT : I guess you have issues including a tabBar controller inside a navigation controller ?

  • Thanks for the answer, but i want to know about the navigation in tabbar controller. I think its not good to have the hierarchy like NavigationController -> tabbar controller -> Navigation controller. Apple will not suggest this. – Vinay Jain Nov 20 '13 at 14:19
  • I have a Root VC in the navigation controller, but I hide it. How to go to root view? – Gank Nov 14 '14 at 09:53