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

查看:426
本文介绍了如何从一个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.


  1. self.navigationController code> nil 如果 self 当前不在导航控制器的堆栈。

  2. 您必须保留(并正确地发布 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];

在最后一行,如果您更改动画 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天全站免登陆