本文介绍了pushViewController是否保留控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难找出是否pushViewController保留控制器,目前我有以下代码(它的工作)...

I am struggling to find out if pushViewController retains the controller, currently I have the following code (which works) ...

ColorController *colorController = [[ColorController alloc] initWithNibName:nibColor bundle:nil];
[[self navigationController] pushViewController:colorController animated:YES];
[colorController release];

但我考虑删除发布并添加自动释放...

but am considering removing the release and adding an autorelease ...

ColorController *colorController = [[[ColorController alloc] initWithNibName:nibColor bundle:nil] autorelease];
[[self navigationController] pushViewController:colorController animated:YES];

非常感谢。

Gary

推荐答案

这不起作用...

ColorController *colorController = [[ColorController alloc] initWithNibName:nibColor bundle:nil];
[[[self navigationController] pushViewController:colorController animated:YES] autorelease];

您正在自动释放pushViewController:animated :, void的返回值。

You are autoreleasing the return value of pushViewController:animated:, which is void.

您的第一个代码段有效,且正确。 pushViewController:确实保留被推送的控制器。

Your first snippet is valid, and correct. pushViewController: does indeed retain the controller that is pushed.

编辑:在更新的代码中,两个样本之间没有什么区别。两者都保持适当的保留计数。但是,除非必要,否则避免使用autoRelease是一种最佳实践(尤其是在内存敏感区域,如iPhone)。这有助于您的应用程序保持更可预测和可管理的内存占用。

In your updated code, there is little difference between the two samples. Both maintain proper retain counts. However, it is a "best practice" to avoid using autoRelease unless necessary (especially in a memory sensitive area, like the iPhone). This helps your application to maintain a more predictable and manageable memory footprint.

这篇关于pushViewController是否保留控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 15:46