UIButton的子视图需要释放吗?

发布于 2024-10-25 09:10:00 字数 595 浏览 6 评论 0原文

第一次在这里提问。

我有一个问题,我有一个 UIImageView 作为子视图添加到我的 UIButton 中,它是使用 buttonWithType: 声明的(这意味着我不'不必释放按钮吗?)但是我仍然需要释放 UIButton 的子视图吗?

代码位:

UIImage *circleImage = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"item-circle" ofType: @"png"]];
UIImageView *circleImageView = [[[UIImageView alloc] initWithImage: circleImage] autorelease];
[imageView setFrame: CGRectMake(-5, -5, 65, 65)];

UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
[button addSubview: circleImageView];

First time asking a question here.

I have a question, I have a UIImageView added as a subview to my UIButton, which is declared using buttonWithType: (which means I don't have to release the button right?) But do I still have to release the subview of my UIButton?

Code bits:

UIImage *circleImage = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"item-circle" ofType: @"png"]];
UIImageView *circleImageView = [[[UIImageView alloc] initWithImage: circleImage] autorelease];
[imageView setFrame: CGRectMake(-5, -5, 65, 65)];

UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
[button addSubview: circleImageView];

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

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

发布评论

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

评论(2

近箐 2024-11-01 09:10:00

简短回答:

您的代码看起来不错。基本的经验法则是,对于每个 allocnewretaincopy,您都需要一个 <代码>释放或<代码>自动释放。


长答案:

让我们逐行浏览您的代码。

UIImage *circleImage = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle]  pathForResource: @"item-circle" ofType: @"png"]];

第一行使用了一种便捷方法。您不需要对任何内容调用release,因为您还没有调用allocnewretaincopy< /代码>。

UIImageView *circleImageView = [[[UIImageView alloc] initWithImage: circleImage] autorelease];

在第二行中,您调用了 alloc,但随后调用了 autoerelease,因此您的情况很好。

[imageView setFrame: CGRectMake(-5, -5, 65, 65)];

同样,没有allocnewretaincopy

UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];

您再次使用了一种便捷方法。

[button addSubview: circleImageView];

您还没有调用 allocnewretaincopy。因此,您不需要调用 releaseautorelease

Short Answer:

Your code seems fine. The basic rule of thumb is that for every alloc, new, retain, or copy, you need a release or autorelease.


Long Answer:

Let's go through your code line by line.

UIImage *circleImage = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle]  pathForResource: @"item-circle" ofType: @"png"]];

This first line uses a convenience method. you don't need to call release on anything, since you haven't called alloc, new, retain, or copy.

UIImageView *circleImageView = [[[UIImageView alloc] initWithImage: circleImage] autorelease];

In the second line, you call alloc, but you then call autoerelease, so you're good there.

[imageView setFrame: CGRectMake(-5, -5, 65, 65)];

Again, no alloc, new, retain, or copy.

UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];

Once again, you've used a convenience method.

[button addSubview: circleImageView];

You still haven't called alloc, new, retain, or copy. Therefore, you don't call release or autorelease.

苦妄 2024-11-01 09:10:00

作为一般规则,您自己分配保留的任何内容都需要释放。但您在这里(本质上)是通过调用 autorelease 来做到这一点的。如果你问是否需要再次释放子视图,答案是否定的。

这同样适用于您的按钮。您尚未调用 allocretain(而是使用 buttonWithType),因此无需调用 release代码>就可以了。

As a general rule, anything you alloc or retain yourself will need to be released. But you are doing that here (in essence) by calling autorelease. If you're asking whether or not you need to release the subview again, the answer is no.

The same applies to your button. You haven't called alloc or retain (instead you used buttonWithType), so you don't need to call release on it.

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