这段代码有什么问题?尝试将 navController 加载到模态视图控制器上

发布于 2024-11-27 11:09:38 字数 922 浏览 4 评论 0原文

我有一个 UIViewcontroller,我想将其推送到 UINavigationController 上,而 UINavigationController 又会使用 PresentModalViewController:animated 推送到模态视图上。

这是我的代码:

TargetViewController *targetViewController = [[[TargetViewController alloc] init] autorelease];
UINavigationController *targetNavController = [[[UINavigationController alloc] initWithRootViewController:targetViewController] autorelease];
[self presentModalViewController:targetNavController animated:YES];

当代码运行时,模态视图按预期加载,但是在调用dismissModalViewControllerAnimated:之后,模态视图向下滑动并且应用程序崩溃。

我在 gdb 中收到以下错误: -[CALayer保留]:发送到已释放实例的消息

问题的第一部分:上面的代码有什么本质上的错误吗?

第二部分:如果上面的代码没有问题,接下来我应该在哪里调试?

其他信息: 当我释放或自动释放navController时,它工作正常。但是 Instruments 会显示废弃的内存,我只能假设 navController 没有被释放。 控制器

也许是模态视图 我知道崩溃与上述 ViewController、navController 和模态视图的内存管理有关,因为我的代码在弄乱此代码之前正在运行。

I have a UIViewcontroller that I want to push onto a UINavigationController, which in turn would push onto a modal view using presentModalViewController:animated.

Here's my code:

TargetViewController *targetViewController = [[[TargetViewController alloc] init] autorelease];
UINavigationController *targetNavController = [[[UINavigationController alloc] initWithRootViewController:targetViewController] autorelease];
[self presentModalViewController:targetNavController animated:YES];

When code is run, the modal view loads as expected, but after dismissModalViewControllerAnimated: is called, the modal view slides down and the app crashes.

I get the following error in gdb:
-[CALayer retain]: message sent to deallocated instance

First part of question: is there anything inherently wrong with the above code?

Second part: if there is nothing wrong with above code, where should I look next to debug?

Additional info:
When I don't release or autorelease the navController, it works fine. But Instruments will show abandoned memory, which I can only assume is the navController not being released. Maybe the modal view controller

P.S. I know that the crash is related to the memory management of the above ViewController, navController and modal view, because my code was working prior to messing with this code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

铁轨上的流浪者 2024-12-04 11:09:38

您通常不会将 navController 作为视图推送,因为导航控制器充当根控制器。视图是从 navController 推送的。一旦推送了一个视图,您就可以以模态方式呈现下一个视图。

You generally don't push a navController as a view as the navigation controller works as the root controller. The views are pushed from the navController. Once you have a view pushed, you could then present the next view modally.

从来不烧饼 2024-12-04 11:09:38

如果不使用 autorelease 会发生什么?

IE:

TargetViewController *targetViewController = [[TargetViewController alloc] init];
UINavigationController *targetNavController = [[UINavigationController alloc] initWithRootViewController:targetViewController];
[targetViewController release];
[self presentModalViewController:targetNavController animated:YES];
[targetNavController release];

What happens if you don't use autorelease?

I.E.:

TargetViewController *targetViewController = [[TargetViewController alloc] init];
UINavigationController *targetNavController = [[UINavigationController alloc] initWithRootViewController:targetViewController];
[targetViewController release];
[self presentModalViewController:targetNavController animated:YES];
[targetNavController release];
弃爱 2024-12-04 11:09:38

解决了这个问题。我正在释放 VC,导致崩溃。感谢您的意见。

Fixed the issue. I was releasing the VC, causing the crash. Thanks for the input.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文