调用 popViewControllerAnimated 两次 [英] Calling popViewControllerAnimated twice

查看:49
本文介绍了调用 popViewControllerAnimated 两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UINavigationController,上面有一系列 UIViewControllers.在某些情况下,我想准确地弹出两个级别.我以为我可以通过连续两次调用 popViewControllerAnimated 来做到这一点,但事实证明,我第二次调用它时,它没有弹出任何东西,而是返回 NULL.我是否需要存储对目标 VC 的引用并改为调用 popToViewControllerAnimated?我可以这样做,但它会使我的代码复杂化,因为我必须在将 VC 推入堆栈时传递 UIViewController*.

I've got a UINavigationController with a series of UIViewControllers on it. Under some circumstances, I want to pop back exactly two levels. I thought I could do it by calling popViewControllerAnimated twice in a row, but it turns out that the second time I call it, it's not popping anything and instead returning NULL. Do I need to store a reference to my destination VC and call popToViewControllerAnimated instead? I can do that, but it complicates my code since I'd have to pass the UIViewController* around as I'm pushing VCs onto the stack.

以下是相关片段:

UIViewController* one = [self.navigationController popViewControllerAnimated:YES];
if (...) {
    // pop twice if we were doing XYZ
    UIViewController *two = [self.navigationController popViewControllerAnimated:YES];
    // stored in "one" and "two" for debugging, "two" is always 0 here.
}

我在这里做了什么奇怪的事情吗?我想写惯用的代码,所以如果正确"的方式是调用 popToViewControllerAnimated,或者完全是别的什么,我会很乐意改变它.

Am I doing something weird here? I want to write idiomatic code, so if the "right" way is to call popToViewControllerAnimated, or something else entirely, I'll happily change it.

推荐答案

在这种情况下,您需要像这样弹出导航控制器中的特定视图控制器:

In this case you would need to pop back to a specific viewcontroller in the navigationController like so:

[self.navigationController popToViewController:[[self.navigationController viewControllers] objectAtIndex:2] animated:YES];

该代码将弹出到 navigationController 堆栈上的第三个视图控制器.

That code would pop to the third viewcontroller on the navigationController's stack.

这篇关于调用 popViewControllerAnimated 两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆