Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move back to the root view controller in storyboard?

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?

like image 970
Vinay Jain Avatar asked Nov 20 '13 14:11

Vinay Jain


People also ask

How do I change the root view controller in storyboard?

1) Remove Storyboards from app. plist file, 2) Uncheck option "isInitialViewController" from Storyboards which is checked in case of Tab Bar controller because its a root ViewController , 3) Add this code in appDelegate. m file.

How do I go back to the previous view controller in Objective C?

To go one view back, use below code. This will ensure that the smooth transition between views are retained. UINavigationController *navigationController = self. navigationController; [navigationController popViewControllerAnimated:NO]; [navigationController popViewControllerAnimated:YES];


2 Answers

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.

like image 85
Vinay Jain Avatar answered Sep 29 '22 06:09

Vinay Jain


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]; 
like image 35
Nikhlesh Bagdiya Avatar answered Sep 29 '22 07:09

Nikhlesh Bagdiya