4

我正在使用 IOS5 故事板。我的视图控制器路径如下:

tabbarVC --> navigationVC-1 --> tableVC-1 --(via segue push)-> tableVC-2 --(via segue modal)-> navigationVC-2 --> tableVC-3

在 tableVC-3 中的取消按钮回调操作方法中,我调用[self dismissViewControllerAnimated:YES completion:nil]; 成功让我回到 tableVC-2。但是,当我尝试在调试器中向后检查导航路径时,我看不到从 navigationVC-2 访问 tableVC-2 的方法。我希望navigationVC-2 保持到tableVC-2 或navigationVC-1 的链接,但似乎没有。请在下面查看我的调试器输出。

有人可以解释导航层次结构以及如何以编程方式向后遍历链吗?

(gdb) po self
<tableVC-3: 0x6d33340>

(gdb) po (UIViewController*) [self navigationController]
<UINavigationController: 0x6d33560>

(gdb) po (UIViewController*)[[self navigationController] navigationController]
Can't print the description of a NIL object.

(gdb) po (UIViewController*)[[self navigationController] topViewController]
<tableVC-3: 0x6d33340>

(gdb) po (UIViewController*)[[self navigationController] presentingViewController]
<UITabBarController: 0x6b2eba0>

(gdb) po (UIViewController*)[[self navigationController] presentedViewController]
Can't print the description of a NIL object.

(gdb) po (UIViewController*)[[self navigationController] visibleViewController]
<tableVC-3: 0x6d33340>
4

4 回答 4

8

这是一个老问题,但只是为了帮助遇到这个问题的其他人,有一个命令可以让你的生活更轻松..

[self.navigationController popToRootViewControllerAnimated:TRUE];

当你偶然发现正确的命令时很容易,不是吗!

因此,假设您在导航控制器中有一系列三个屏幕,并且在第三个屏幕上您希望“返回”按钮将您带回初始屏​​幕。

-(void)viewDidLoad
{
    [super viewDidLoad];

    // change the back button and add an event handler
    self.navigationItem.leftBarButtonItem =
    [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                     style:UIBarButtonItemStyleBordered
                                    target:self
                                    action:@selector(handleBack:)];
}


-(void)handleBack:(id)sender
{
    NSLog(@"About to go back to the first screen..");
    [self.navigationController popToRootViewControllerAnimated:TRUE];
}
于 2012-09-13T11:53:04.373 回答
4

要抛出一个旧的响应,很难更新它以使其更完整。
要解决这个问题:

有人可以解释导航层次结构以及如何以编程方式向后遍历链吗?

您的导航结构:

tabbarVC --> navigationVC-1 --> tableVC-1 --(通过segue push)-> tableVC-2 --(通过segue modal)-> navigationVC-2 --> tableVC-3

可以这样解释:
TabbarVC 显示它是“selectedViewController”(navigationVC-1)。
NavigationVC-1 的导航堆栈由 TableVC-1 和 TableVC-2 组成(NavigagtionVC-1 的 topViewController)
然后 NavigationVC-2 在 tabbarVC 上模态呈现,因此 tabbarVC 是presentingViewController而 NavigationVC-2 是presentedViewController

因此,为了从 tableVC-3 到达 tableVC-2,您需要执行以下操作:

[(UINavigationController *)[(UITabBarController *)[tableVC-3 presentingViewController] selectedViewController] topViewController];

(不要在生产代码中这样做)

[tableVC-3 presentingViewController]以及[tableVC-3.navigationController presentingViewController]会还给你的UITabBarController


如果您使用的是UINavigationController,您应该使用它的push方法pop来将 UIViewController 置于“演示堆栈”上或之外。
您将能够UINavigationController从以下位置访问UIViewController

self.navigationController

如果您想在“演示堆栈”上返回多个,UIViewController您可以在UINavigationController

popToViewController:animated:弹出
视图控制器,直到指定的视图控制器位于导航堆栈的顶部。
- (NSArray *)popToViewController:(UIViewController *)viewController 动画:(BOOL)动画


要关闭以模态方式呈现的UIViewControllerUIViewController ,已经呈现它的 UIViewController 需要使用以下命令将其关闭:

- (void)dismissModalViewControllerAnimated:(BOOL)animated

所以在这种情况下应该是:

[tableVC-2 dismissModalViewControllerAnimated:YES];
于 2012-01-02T05:45:53.903 回答
4

在故事板中使用这个和其他几个关于模态 UIViewControllers 的问题进行一些研究后,返回我使用的两个视图

[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
于 2013-07-31T08:44:29.850 回答
0

迅速

如果您使用的是导航控制器,您可以使用导航回上一个视图控制器

self.navigationController?.popViewControllerAnimated(true)

或返回根视图控制器

self.navigationController?.popToRootViewControllerAnimated(true)
于 2016-06-10T05:01:08.847 回答