ios - popToRootViewControllerAnimated 的问题

标签 ios uinavigationcontroller uistoryboard uistoryboardsegue

在我的 StoryBoard 中,我将 Root View Controller 嵌入到导航 Controller 中,该 View 在应用程序启动时显示。然后用户浏览一系列 View ,这些 View 基本上是模态呈现的 View Controller 。

我正在尝试实现一个返回到 Root View Controller 的函数,所以我调用了

-(IBAction)backToMenu{
    NSLog(@"Back to menu");
    [self.navigationController popToRootViewControllerAnimated:YES];
}

但是没有任何反应。如果我执行 NSLog(@"%@", self.navigationController"); 它会打印 null,所以我想这就是我的问题的根源。你不能调用popToRootViewControllerAnimated: 在以模态方式呈现的 View Controller 上。

除非您传递对 Root View Controller 的引用。但这是正确的做法吗?如果是这样,您如何传递对 Root View Controller 的引用?由于我所有的 View Controller 都是 UIViewController 的自定义子类的实例,因此我尝试将其插入到所述类的代码中:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    [[segue destinationViewController] navigationController] = [[segue sourceViewController] navigationController];    
}

但我收到一条错误消息,指出 navigationController 不可分配。

有什么想法吗?

最佳答案

dismissViewControllerAnimated:completion: 的文档中,它说:

If you present several view controllers in succession, thus building a stack of presented view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack.

这表明您应该保留对 Root View Controller 的引用(或以其他方式通知它)并在其上调用此方法。 (在已弃用的 dismissModalViewControllerAnimated: 上有类似的说明,以防您使用它。)

关于ios - popToRootViewControllerAnimated 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10740469/

相关文章:

ios - 如何在按下按钮时从 UITextField 更新标签

ios - GLSL 程序在某些 iOS 硬件上间歇性失败

ios - 使用NSDictionaryResultType获取属性时,是否可以将sectionKeyPath与NSFetchedResultsController一起使用?

iphone - UINavigationController 自定义 popViewControllerAnimated 方法

swift3 - 无法实例化 UIStoryboard(名称 : "something", bundle :nil)

ios - 在 iOS 上检索多个电话联系人标签

ios - 如何从导航 Controller 转换回 VC?

ios - 无法在 iOS 中创建新的标签栏 Controller

ios - 滑动返回只能在屏幕边缘使用吗?

swift - 如何设置新的 Root View Controller