UIView内存释放

时间:2011-10-25 04:56:37

标签: iphone ios ios4

我何时必须在以下代码中发布UIView

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

return headerView;

感谢您的帮助

enter image description here

2 个答案:

答案 0 :(得分:3)

将其作为自动释放返回。使用此功能的人将负责其所有权。 See this post

答案 1 :(得分:3)

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

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

例如:

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