UIView内存释放

发布于 2024-12-11 21:07:57 字数 259 浏览 0 评论 0原文

我什么时候必须在下面的代码中释放 UIView

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(20, 10,
120.0, 100)]; 

return headerView;

感谢您的帮助

在此处输入图像描述

When do I have to release UIView in the below code?

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(20, 10,
120.0, 100)]; 

return headerView;

Thanks for any help

enter image description here

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

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

发布评论

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

评论(2

椵侞 2024-12-18 21:07:57

这取决于您将如何使用该视图。如果将从方法返回的视图分配给某个对象(例如 UIViewController)并且该对象保留它,那么您应该在上述函数中自动释放该视图。这样,您将确保它在方法循环结束后自动释放,并且它的活动时间足以让对象保留它。所以代码将是这样的:

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(20, 10,
120.0, 100)]; 
[headerView autorelese];
return headerView;

例如:

myViewController.view=theMethodThatReturnsView;//which is the above method

It depends on how you're going to use the view. If you assign the view returned from the method to some object (for example to a UIViewController) and that object retains it then you should autorelese the view in the above function. This way you'll make sure that it gets released automatically after the method loop ends and also it'll be alive long enough for an object to retain it. So the code will be like this:

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(20, 10,
120.0, 100)]; 
[headerView autorelese];
return headerView;

For ex:

myViewController.view=theMethodThatReturnsView;//which is the above method
じ违心 2024-12-18 21:07:57

将其作为自动释放返回。使用此功能的人将负责其所有权。 查看此内容发布

return it as auto-release. The one who uses this functions will take care of its ownership. See this post.

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