12

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

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

2) ThirdViewController 以模态方式呈现

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

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

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

[self.navigationController popToRootViewControllerAnimated:YES]; 

只有这个人回到 SecondViewController 一次

[self.navigationController popViewControllerAnimated:YES];

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

4

3 回答 3

21

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

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

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

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

@protocol ModalViewControllerDelegate
- (void)modalViewControllerDidCancel;
@end

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

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

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

于 2013-05-29T23:41:03.967 回答
4

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

于 2013-05-29T23:57:14.857 回答
0

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

AppDelegate *appDel = (AppDelegate*)[[UIApplication sharedApplication] delegate];
RootViewController *rootView = [[RootViewController alloc] init];
[appDel.window setRootViewController:rootView];
于 2013-05-29T23:10:51.267 回答