我有一个上面有一系列UIViewControllers的UINavigationController。在某些情况下,我希望弹出两个级别。我想我可以通过连续两次调用popViewControllerAnimated来实现,但是第二次调用它时,它并没有弹出任何东西,而是返回NULL。我是否需要存储对目标VC的引用并调用popToViewControllerAnimated?我可以这样做,但这会使我的代码变得复杂,因为当我将VC推送到堆栈上时,我必须传递UIViewController*。
下面是相关的代码片段:
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,或者完全调用其他东西,我会很乐意改变它。
发布于 2009-07-14 01:16:44
在这种情况下,您需要弹回navigationController中的特定视图控制器,如下所示:
[self.navigationController popToViewController:[[self.navigationController viewControllers] objectAtIndex:2] animated:YES];该代码将弹出到navigationController堆栈上的第三个viewcontroller。
发布于 2011-09-15 23:13:46
我认为最好是计算堆栈中视图控制器的数量,然后减去想要弹出的视图控制器的数量。
NSInteger noOfViewControllers = [self.navigationController.viewControllers count];
[self.navigationController
popToViewController:[self.navigationController.viewControllers
objectAtIndex:(noOfViewControllers-2)] animated:YES];使用此解决方案,如果您稍后将新视图添加到您的项目中,则不会弄乱弹出窗口。
发布于 2013-07-13 10:44:27
如果您保存对UINavigationViewController的引用并使用保存的实例,则对我有效:
UINavigationViewController* savedUinvc = self.navigationController;
UIViewController* one = [savedUinvc popViewControllerAnimated:YES];
if (...) {
// pop twice if we were doing XYZ
UIViewController *two = [savedUinvc popViewControllerAnimated:YES];
// stored in "one" and "two" for debugging, "two" is always 0 here.
}https://stackoverflow.com/questions/1122988
复制相似问题