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

天涯浪子 提交于 2019-12-03 07:20:37

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.

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.

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]; 

This should be enough.

[self.navigationController popToRootViewControllerAnimated:YES];

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];

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 ?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!