在我的 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/