简体   繁体   English

如何在故事板中移回根视图控制器?

[英]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. 要求是从VC1导航回根视图控制器。 How can we achieve this? 我们怎样才能做到这一点?

I got the resolution to the issues using the "Unwind Segues". 我使用“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): 步骤1)您需要的最小值是为目标视图子类化视图控制器(也就是先前在导航中弹出的视图,并且您想要展开它)并向其添加这样的方法(方法名称)可以是你想要的任何东西,但它应该是唯一的,因为整个应用程序中的所有展开segue都列在一起):

- (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. 步骤2)现在,在您的源视图(也就是您想要放松的视图)中,您只需将按钮中的segue或任何向下拖动到源视图底部的绿色“EXIT”图标。 There should now be an option to connect to "- unwindToViewControllerNameHere" 现在应该有一个连接到“ - unwindToViewControllerNameHere”的选项

That's it, your segue will unwind when your button is tapped. 就是这样,当您点击按钮时,您的segue将会放松。 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 | 根VC | --- tabbar controller | --- tabbar控制器| --- Navigation Controller -- | ---导航控制器 - | --- VC1 --- VC1

and there is a UIButton on VC1, so on click of that you want to move to root viewcontroller (Root VC) then use : 并且在VC1上有一个UIButton ,所以点击你想要移动到root viewcontroller(Root VC),然后使用:

-(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. 这里mainnav是根mainnav控制器导航控制器的故事板标识符。

ViewControllers层次结构

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. 根据图片白色viewcontroller是一个根视图控制器,tabBarController有2个带导航控制器的选项卡,如果你想从第二个选项卡视图控制器UIButton (黑色)移动到根视图控制器,请点击然后使用上面的代码。

If you have hierarchy like --- Navigation Controller -- | 如果您有层次结构,如--- Navigation Controller - | Root VC | 根VC | --- VC1---- |--- VC2---- | --- VC1 ---- | --- VC2 ---- |

and want to move to root viewcontroller (Root VC) from VC1 or from VC2 then use : 并希望从VC1或从VC2移动到根视图控制器(根VC),然后使用:

[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 调用方法名称为returnToRoot

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

OR 要么

[self.navigationController popToRootViewControllerAnimated:YES];

这应该足够了。

[self.navigationController popToRootViewControllerAnimated:YES];

You should enable the Root VC in the navigation controller : 您应该在导航控制器中启用Root VC:

--- Navigation Controller -- | ---导航控制器 - | Root VC | 根VC | --- tabbar controller | --- tabbar控制器| --- VC1 --- VC1

And then call 然后打电话

[navController popToRootViewControllerAnimated:YES];

EDIT : I guess you have issues including a tabBar controller inside a navigation controller ? 编辑:我猜你有一些问题,包括导航控制器内的tabBar控制器?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM