简体   繁体   English

UIView内存版本

[英]UIView memory release

When do I have to release UIView in the below code? 什么时候必须在下面的代码中释放UIView

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

return headerView;

Thanks for any help 谢谢你的帮助

在此处输入图片说明

return it as auto-release. 以自动发布的形式返回。 The one who uses this functions will take care of its ownership. 使用此功能的人将注意其所有权。 See this post . 看到这篇文章

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. 如果将方法返回的视图分配给某个对象(例如,分配给UIViewController),并且该对象保留了该对象,则应在上述函数中自动关联视图。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM