iPhone NavigationController清除视图堆栈 [英] iphone NavigationController clear views stack

查看:79
本文介绍了iPhone NavigationController清除视图堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用导航控制器的iPhone应用程序.在该控制器中,我提出了一些意见.在某些情况下,我想清除"视图堆栈,仅将导航控制器的rootViewController留在堆栈中,然后推入我拥有的另一个viewController.

I have an iphone application that uses a navigation controller. In that controller I push some views. In some cases I want to "clear" the views stack, leave only the rootViewController of the navigation controller in the stack and push another viewController I have.

有人可以给我举一个例子吗?我看不到任何清除堆栈的方法.

Can someone give me an example on how to do this? I don't see any method that clears the stack.

答案1: 我尝试将以下代码放入操作"按钮:

Answer 1: I have tried to put in button Action the following code:

[self.navigationController popToRootViewControllerAnimated:NO]; 

 do some stuff here to prepare for the push.

[self.navigationController pushViewController:self.myOtherController animated:YES];

,但仅弹出到roorController.它不会推送我想要的另一个viewController.

but it only pops to the roorController. It doesn't push the other viewController I want.

推荐答案

下面的代码将允许用户向下钻取视图层次结构,然后按一下按钮,弹出到根视图控制器并推送一个新的视图.

The following code will allow the user to drill down a view hierarchy, and at the press of a button, pop back to the root view controller and push a new view.

DetailViewController.m〜从中清除导航堆栈的视图控制器:

DetailViewController.m ~ the view controller from which to clear the navigation stack:

- (IBAction)buttonPressed:(id)sender {
    [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"popBack" object:nil]];
}

上面的代码调用了NSNotificationCenter,并发布了一个通知,通知RootViewController听到时可以做出反应.但是首先,RootViewController必须注册才能接收通知.

The above code makes a call to NSNotificationCenter, posting a notification that the RootViewController can react to when heard. But first, the RootViewController must register to receive the notification.

RootViewController.m

RootViewController.m

- (void)viewDidLoad
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushViews) name:@"popBack" object:nil];
    [super viewDidLoad];
}

接下来,RootViewController必须设置引用的选择器-pushViews.

Next, the RootViewController must set up the referenced selector -pushViews.

- (void)pushViews {
     //Pop back to the root view controller
     [self.navigationController popToRootViewControllerAnimated:NO];

     //Allocate and init the new view controller to push to
     NewViewController *newVC = [[NewViewController alloc] init];

     //Push the new view controller
     [self.navigationController pushViewController:newVC animated:YES];
}

请确保在调用-popToRootViewControllerAnimated:时为动画指定NO.启用动画会导致导航栏动画出现打and,并使系统混乱.上面的代码在被调用时将清除导航堆栈,仅保留RootViewController,然后添加NewViewController.

Be sure that when you call -popToRootViewControllerAnimated:, you specify NO for animation. Enabling animation causes hiccups in the navigation bar animation and confuses the system. The above code, when called, will clear the navigation stack, leaving only the RootViewController, and then adding the NewViewController.

您的初始代码未完全执行的原因是,从DetailViewController调用-popToRootViewController:之后,RootViewController的方法占据了主线程,并且DetailViewController被释放.因此,没有从该视图控制器运行其他代码.使用上面的代码,导航堆栈会弹出回到正在加载的同一视图控制器.

The reason your initial code was not fully executing was because after calling -popToRootViewController: from your DetailViewController, the RootViewController's methods occupied the main thread, and the DetailViewController was released. Thus, no further code was run from that view controller. Using the code above, the navigation stack is popping back to the same view controller that is being loaded.

这篇关于iPhone NavigationController清除视图堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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