我应该释放 self.view 吗?

发布于 2024-09-11 14:06:36 字数 339 浏览 2 评论 0原文

我有一个关于 UIViewController 中 self.view 的问题。

首先,在我的应用程序中,一切都是以编程方式创建的。通常我在loadView方法中创建self.view:

self.view = [[UIView alloc]initWithFrame:SCREEN_FRAME]autorelease]; // SCREEN_FRAME is a constant

此时,self.view的保留计数为1。

所以,我的问题是,当我使用完视图控制器时是否必须释放self.view?如果是这样,我应该在哪里释放它?

预先非常感谢:)

I have a question regarding to self.view in a UIViewController.

First, in my app, everything is created programmatically. And normally I create self.view in the loadView method:

self.view = [[UIView alloc]initWithFrame:SCREEN_FRAME]autorelease]; // SCREEN_FRAME is a constant

At this moment, the retain count of self.view is 1.

So, my question is, do I have to release self.view when I'm done with the view controller? If so, where should I release it?

Thanks very much in advance :)

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

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

发布评论

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

评论(3

撕心裂肺的伤痛 2024-09-18 14:06:36

这是通过 UIViewController 的实现为您完成的,只需确保在 dealloc 方法中调用 [super dealloc] 即可。

That is being done for you by the implementation of UIViewController, just make sure you call [super dealloc] in your dealloc method.

岁月无声 2024-09-18 14:06:36

self.view添加了自动释放池,当池被释放时,对象也会被释放。你不需要明确地释放。
如果您将对象添加到池中并手动释放,您将得到双重释放异常(因为对象被释放两次)

self.view is added autorelease pool and object will be release when pool is released. you don't need to release explicitly.
If you add object to pool and release manually you will get exception double dealloc (since object is getting released twice)

爱,才寂寞 2024-09-18 14:06:36

仅供阅读本文的人参考,上述答案并不完全正确。 self.view 被称为 alloc、retain 和 autorelease,总保留计数为 1。retain 调用来自retain 属性和点语法。

因此它确实需要被释放,但正如 willcodejavaforfood 指出的那样,dealloc 的超级实现做到了这一点。

谢谢

Just for the reference of whoever might read this, the above answer is not exactly correct. self.view is called alloc, retain and autorelease, for the total retain count of 1. The retain call comes from the retain property and the dot syntax.

Therefore it DOES need to be released, but as willcodejavaforfood points out, the super implementation of dealloc does that.

Thanks

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