【问题标题】:Back to RootViewController from Modal View Controller从 Modal View Controller 返回 RootViewController
【发布时间】:2013-05-25 09:13:59
【问题描述】:

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

1) SecondViewController 由我的 Storyboard 中连接的按钮推动

2) ThirdViewController 模态呈现

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

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

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

[self.navigationController popToRootViewControllerAnimated:YES]; 

只有这家伙回到 SecondViewController 一次

[self.navigationController popViewControllerAnimated:YES];

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

【问题讨论】:

  • 我认为你想做的是在你的thirdViewController:[self.presentingViewControllerdismissViewControllerAnimated:NO]; [self.presentingViewController popToRootViewControllerAnimated:YES];,请原谅我在手机上打字不好
  • Return to root view in IOS 的可能重复项

标签: ios objective-c uiviewcontroller uinavigationcontroller storyboard


【解决方案1】:

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

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

- (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: 并将YES 传递给popToRootViewControllerAnimated: 以获得从模态视图到根视图的更流畅的动画。

【讨论】:

  • 我正在测试您的解决方案。委托方式更复杂,但也许值得一试。
  • 您的回答帮助了我。我只需要通过发布通知而不是委托来做到这一点
  • 最后第一行是 [self dismissModalViewControllerAnimated:NO];
  • @Vad 我很高兴你能成功。 dismissModalViewControllerAnimated: 方法在 iOS 6.0 中已被弃用。现在推荐使用的方法是dismissViewControllerAnimated:completion:, see doco here。
  • 谢谢,我花了很多时间来解决一个类似的问题。没有任何效果!但是这个简单的解决方案解决了问题。
【解决方案2】:

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

【讨论】:

  • 很高兴知道它,但必须支持 iOS5 :(
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    相关资源
    最近更新 更多