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

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

问题描述

我想将视图控制器推入堆栈,然后弹出第一个推动新视图控制器。

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

//弹出这个VC,怎么样?
}

编辑:结果我可以弹出2个视图控制器,一旦完成新的VC。仍然不是我想要的,但它确实有效。缺点是我需要设置一个标志来表明覆盖的视图已经完成。

解决方案

这是一种弹回两个技术查看控制器,当你第一次弹出时,你的当前视图控制器的类似问题及其navigationController属性就会消失:

  //将堆栈上的2个控制器弹回设置屏幕
//

//本地存储导航控制器,因为
// self.navigationController将是一旦我们弹出
//
UINavigationController * navController = self.navigationController;

//保留自己,以便控制器在弹出
//
[[self retain] autorelease]后仍然存在;

//将2个控制器弹出到设置界面
//
[navController popViewControllerAnimated:NO];
[navController popViewControllerAnimated:YES];

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


setViewControllers:animated:用导航控制器用
指定的项目替换目前管理
的视图控制器
。 / p>


  • (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated参数
    viewControllers视图控制器
    放在堆栈中。此数组中控制器

    前后顺序表示导航堆栈中控制器
    的新
    自下而上的顺序。因此,添加到数组的
    最后一项变为
    导航堆栈的顶部项目。
    animated如果是,则按下
    或弹出顶视图控制器的动画。
    如果为NO,则替换视图控制器
    ,不带任何动画。讨论您
    可以使用此方法更新或
    替换当前视图控制器
    堆栈,而无需显式推送或弹出每个
    控制器。此外,
    此方法允许您更新
    控制器的集合,而无需动画
    更改,这可能适用于
    启动时间,当您想要返回
    时导航控制器到
    之前的状态。



如果启用了动画,则此方法
决定哪种类型的过渡
根据items数组中的最后一项
是否已经在
导航堆栈中执行。如果视图
控制器当前在堆栈中,
但不是最顶层的项目,则此
方法使用弹出转换;如果
是最上面的项目,则没有转换是
。如果视图控制器是
不在堆栈上,则此方法使用
推送转换。只执行一次转换
,但当转换
结束时,
堆栈的全部内容将替换为新视图
控制器。例如,如果
控制器A,B和C位于
堆栈上并且您设置了控制器D,A,
和B,则此方法使用pop
转换和结果堆栈
包含控制器D,A和B.



可用性在iOS 3.0和以后的
中可用。声明在
UINavigationController.h


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

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


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?
}

EDIT: 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.

解决方案

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: Replaces the view controllers currently managed by the navigation controller with the specified items.

  • (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated Parameters viewControllers The view controllers to place in the stack. The front-to-back order of the controllers in this array represents the new bottom-to-top order of the controllers in the navigation stack. Thus, the last item added to the array becomes the top item of the navigation stack. animated If YES, animate the pushing or popping of the top view controller. If NO, replace the view controllers without any animations. Discussion You can use this method to update or replace the current view controller stack without pushing or popping each controller explicitly. In addition, this method lets you update the set of controllers without animating the changes, which might be appropriate at launch time when you want to return the navigation controller to a previous state.

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.

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

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;

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

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