ios 从模态视图控制器返回到 RootViewController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16825261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:43:18  来源:igfitidea点击:

Back to RootViewController from Modal View Controller

iosobjective-cuiviewcontrolleruinavigationcontrollerstoryboard

提问by Vad

From Home view - my RootViewController - I open up 2 ViewControllers one after another as user progresses in navigation hierarchy like so:

从主页视图 - 我的 RootViewController - 随着用户在导航层次结构中的进展,我一个接一个地打开 2 个 ViewControllers,如下所示:

1) SecondViewController is pushed by button connected in my Storyboard

1)SecondViewController 由我的故事板中连接的按钮推动

2) ThirdViewController is presented modally

2) ThirdViewController 模态呈现

[self performSegueWithIdentifier:@"NextViewController" sender:nil];

So, the picture is: RootViewController -> SecondViewController -> ThirdViewController

所以,图片是:RootViewController -> SecondViewController -> ThirdViewController

Now in my ThirdViewController I want to have a button to go back 2 times to my RootViewController, i.e. go home. But this does not work:

现在在我的 ThirdViewController 中,我想要一个按钮返回 2 次到我的 RootViewController,即回家。但这不起作用:

[self.navigationController popToRootViewControllerAnimated:YES]; 

Only this guy goes back once to SecondViewController

只有这个人回到了 SecondViewController

[self.navigationController popViewControllerAnimated:YES];

How can I remove both modal and pushed view controllers at the same time?

如何同时删除模态和推送视图控制器?

回答by Steph Sharp

I had a similar situation, where I had a number of view controllers pushed onto the navigation controller stack, and then the last view was presented modally. On the modal screen, I have a Cancel button that goes back to the root view controller.

我遇到过类似的情况,我将许多视图控制器推送到导航控制器堆栈上,然后以模态方式呈现最后一个视图。在模式屏幕上,我有一个返回到根视图控制器的取消按钮。

In the modal view controller, I have an action that is triggered when the Cancel button is tapped:

在模态视图控制器中,我有一个在点击取消按钮时触发的操作:

- (IBAction)cancel:(id)sender
{
    [self.delegate modalViewControllerDidCancel];
}

In the header of this modal view controller, I declare a protocol:

在这个模态视图控制器的头部,我声明了一个协议:

@protocol ModalViewControllerDelegate
- (void)modalViewControllerDidCancel;
@end

And then the last view controller in the navigation stack (the one that presented the modal view) should implement the ModalViewControllerDelegateprotocol:

然后导航堆栈中的最后一个视图控制器(呈现模态视图的那个)应该实现ModalViewControllerDelegate协议:

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

This method above is the important part. It gets the presenting view controller to dismiss the modal view, and then it pops back to the root view controller. Note that I pass NOto dismissViewControllerAnimated: and YESto popToRootViewControllerAnimated: to get a smoother animation from modal view to root view.

上面的这个方法是重要的部分。它让呈现视图控制器关闭模态视图,然后弹回根视图控制器。请注意,我传递NO给dismissViewControllerAnimated: 和YESpopToRootViewControllerAnimated: 以获得从模态视图到根视图的更平滑的动画。

回答by cfc16

I had the same requirement but was using custom segues between the view controllers. I came across with the concept of "Unwind Segue" which I think came with iOS6. If you are targeting iOS6 and above these links might help: What are Unwind segues for and how do you use them?http://chrisrisner.com/Unwinding-with-iOS-and-StoryboardsThanks.

我有相同的要求,但在视图控制器之间使用自定义转场。我遇到了“Unwind Segue”的概念,我认为它是 iOS6 附带的。如果您的目标是 iOS6 及以上,这些链接可能会有所帮助: Unwind segues 是什么以及如何使用它们?http://chrisrisner.com/Unwinding-with-iOS-and-Storyboards谢谢。

回答by Adam Richardson

Assuming your AppDelegate is called AppDelegate, then you can do the following which will reset the rootviewcontroller for the app window as the view RootViewController

假设您的 AppDelegate 被称为 AppDelegate,那么您可以执行以下操作,将应用程序窗口的 rootviewcontroller 重置为视图 RootViewController

AppDelegate *appDel = (AppDelegate*)[[UIApplication sharedApplication] delegate];
RootViewController *rootView = [[RootViewController alloc] init];
[appDel.window setRootViewController:rootView];