调用popViewControllerAnimated两次 [英] Calling popViewControllerAnimated twice

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

问题描述

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

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.

推荐答案

在这种情况下,您需要在navigationController中弹回一个特定的viewcontroller,如下所示:

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堆栈上的第三个viewcontroller。

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

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

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