gpt4 book ai didi

iphone - 如何在一次操作中从 UINavigationController 中弹出一个 View 并将其替换为另一个 View ?

转载 作者:行者123 更新时间:2023-12-03 18:08:38 25 4
gpt4 key购买 nike

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

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

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

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

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

谢谢,马特

最佳答案

我发现您根本不需要手动弄乱 viewControllers 属性。基本上有两个棘手的事情。

    如果 self 当前不在导航 Controller 的堆栈上,
  1. self.navigationController 将返回 nil。因此,在您失去对它的访问权限之前,请将其保存到局部变量中。
  2. 您必须保留(并正确释放)self,否则拥有您所在方法的对象将被释放,从而导致奇怪的情况。

完成准备工作后,只需像平常一样弹出和推送即可。此代码将立即用另一个 Controller 替换顶部 Controller 。

// 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,那么新屏幕将实际以动画形式显示,并且您刚刚弹出的 Controller 将以动画形式显示出来。看起来很不错!

关于iphone - 如何在一次操作中从 UINavigationController 中弹出一个 View 并将其替换为另一个 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/410471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com