如何在推送的视图控制器下方弹出视图控制器?

How do I pop the view controller underneath a pushed view controller?(如何在推送的视图控制器下方弹出视图控制器?)

本文介绍了如何在推送的视图控制器下方弹出视图控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个视图控制器压入堆栈,然后弹出第一个压入新的视图控制器.

I want to push a view controller onto the stack, then pop the first one that pushed the new one.

-(void) someMethod {
    MegaSuperAwesomeViewController *tempVC = [[MegaSuperAwesomeViewController alloc] init];
    [self.navigationController pushViewController:tempVC animated:YES];
    [tempVC release];

    // pop this VC, how?
}

事实证明,一旦完成新的 VC,我可以弹回 2 个视图控制器.仍然不是我想要的,但它确实有效.缺点是我需要设置一个标志来指示覆盖视图已完成.

turns out I can pop back 2 view controllers instead once finished with the new VC. Still not what I wanted exactly, but it works. The downside is I need to set a flag to indicate that the covered view is completed.

推荐答案

这是一种弹回两个视图控制器的技术,它有一个类似的问题,即当前视图控制器和它的navigationController 属性一旦你这样做就会消失第一个流行音乐:

Here's a technique of popping back two view controllers, which has a similar problem of yours of the current view controller and its navigationController property going away as soon as you do the first pop:

// pop back 2 controllers on the stack to the setup screen
//

// locally store the navigation controller since
// self.navigationController will be nil once we are popped
//
UINavigationController *navController = self.navigationController;

// retain ourselves so that the controller will still exist once it's popped off
//
[[self retain] autorelease];

// Pop back 2 controllers to the setup screen
//
[navController popViewControllerAnimated:NO];
[navController popViewControllerAnimated:YES];

或者,您可以直接在视图控制器的导航控制器堆栈上聚会":

alternatively, you can directly "party" on the navigation controllers stack of view controllers:

setViewControllers:animated: 替换当前管理的视图控制器由导航控制器与指定的项目.

setViewControllers:animated: Replaces the view controllers currently managed by the navigation controller with the specified items.

  • (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated 参数viewControllers 视图控制器放置在堆栈中.这控制器从前到后的顺序在这个数组中代表新的控制器自下而上的顺序在导航堆栈中.就这样添加到数组中的最后一项变为导航堆栈的顶部项目.动画如果是,动画推动或弹出顶视图控制器.如果否,更换视图控制器没有任何动画.讨论你可以使用此方法更新或替换当前视图控制器堆栈而不推入或弹出每个明确的控制器.此外,此方法可让您更新没有动画的控制器变化,这可能是适当的您想返回的启动时间导航控制器到以前的状态.

如果启用了动画,这个方法决定过渡到哪种类型根据是否最后一项执行在 items 数组中已经在导航堆栈.如果视图控制器当前在堆栈中,但不是最上面的项目,这个方法使用弹出过渡;如果是最上面的项目,没有过渡是执行.如果视图控制器是不在堆栈上,此方法使用推动过渡.只有一个过渡被执行,但是当那个过渡完成后,整个内容堆栈被新视图替换控制器.例如,如果控制器 A、B 和 C 位于堆栈并设置控制器 D、A、和 B,此方法使用 pop转换和结果堆栈包含控制器 D、A 和 B.

If animations are enabled, this method decides which type of transition to perform based on whether the last item in the items array is already in the navigation stack. If the view controller is currently in the stack, but is not the topmost item, this method uses a pop transition; if it is the topmost item, no transition is performed. If the view controller is not on the stack, this method uses a push transition. Only one transition is performed, but when that transition finishes, the entire contents of the stack are replaced with the new view controllers. For example, if controllers A, B, and C are on the stack and you set controllers D, A, and B, this method uses a pop transition and the resulting stack contains the controllers D, A, and B.

可用性 适用于 iOS 3.0 和之后.宣布于UINavigationController.h

Availability Available in iOS 3.0 and later. Declared In UINavigationController.h

因此,要在导航堆栈上直接在您下方的视图控制器消失",在视图控制器的 viewDidLoad 中,您可以这样做:

So, to "disappear" the view controller directly under you on the navigation stack, in your view controller's viewDidLoad, you could do this:

NSMutableArray *VCs = [self.navigationController.viewControllers mutableCopy];
[VCs removeObjectAtIndex:[VCs count] - 2];
self.navigationController.viewControllers = VCs;

这篇关于如何在推送的视图控制器下方弹出视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在推送的视图控制器下方弹出视图控制器?

基础教程推荐