Calling popViewControllerAnimated twice(调用 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 两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:调用 popViewControllerAnimated 两次
基础教程推荐
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
