如何在一次操作中从 UINavigationController 弹出视图并将其替换为另一个视图? [英] How can I pop a view from a UINavigationController and replace it with another in one operation?

查看:17
本文介绍了如何在一次操作中从 UINavigationController 弹出视图并将其替换为另一个视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我需要从 UINavigationController 的堆栈中删除一个视图并将其替换为另一个视图.情况是第一个视图创建了一个可编辑的项目,然后用该项目的编辑器替换自身.当我在第一个视图中执行明显的解决方案时:

I have an application where I need to remove one view from the stack of a UINavigationController and replace it with another. The situation is that the first view creates an editable item and then replaces itself with an editor for the item. When I do the obvious solution within the first view:

MyEditViewController *mevc = [[MYEditViewController alloc] initWithGizmo: gizmo];

[self retain];
[self.navigationController popViewControllerAnimated: NO];
[self.navigationController pushViewController: mevc animated: YES];
[self release];

我的行为很奇怪.通常会出现编辑器视图,但如果我尝试使用导航栏上的后退按钮,我会得到额外的屏幕,有些是空白的,有些只是搞砸了.标题也变得随机了.就像导航堆栈被完全冲洗一样.

I get very strange behavior. Usually the editor view will appear, but if I try to use the back button on the nav bar I get extra screens, some blank, and some just screwed up. The title becomes random too. It is like the nav stack is completely hosed.

解决这个问题的更好方法是什么?

What would be a better approach to this problem?

谢谢,马特

推荐答案

我发现您根本不需要手动处理 viewControllers 属性.基本上有两个棘手的事情.

I've discovered you don't need to manually mess with the viewControllers property at all. Basically there are 2 tricky things about this.

    如果 self 当前不在导航控制器的堆栈中,
  1. self.navigationController 将返回 nil.因此,在您无法访问它之前,将其保存到局部变量中.
  2. 你必须retain(并正确地release)self 否则拥有你所在方法的对象将被释放,导致奇怪.
  1. self.navigationController will return nil if self is not currently on the navigation controller's stack. So save it to a local variable before you lose access to it.
  2. You must retain (and properly release) self or the object who owns the method you are in will be deallocated, causing strangeness.

一旦你做好了准备,就可以像往常一样弹出和推送.此代码将立即用另一个控制器替换顶部控制器.

Once you do that prep, then just pop and push as normal. This code will instantly replace the top controller with another.

// 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 this controller and replace with another
[navController popViewControllerAnimated:NO];
[navController pushViewController:someViewController animated:NO];

在最后一行中,如果您将 animated 更改为 YES,那么新屏幕将实际进入动画,而您刚刚弹出的控制器将动画退出.挺好看的!

In that last line if you change the animated to YES, then the new screen will actually animate in and the controller you just popped will animate out. Looks pretty nice!

这篇关于如何在一次操作中从 UINavigationController 弹出视图并将其替换为另一个视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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