iphone - UIView 内存释放

标签 iphone ios ios4

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

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

return headerView;

谢谢你的帮助

enter image description here

最佳答案

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

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

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

关于iphone - UIView 内存释放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7884862/

相关文章:

ios - UIView 未调整大小以填充 iPhone 6 窗口

iphone - “EXC_BAD_ACCESS” , “-[CFString release]: message sent to deallocated instance” 在表格中滚动时

ios - 快速更改 UImenu 的位置

ios - 将数据与 ContentsOfUrl 一起使用

ios - 如何将自定义标签与标签栏项目标题对齐?

iphone - iOS 核心数据 - 插入前如何检查

ios - MFMailComposeViewController 和 MFMessageComposeViewController 在 iOS 8 中旋转后边界有误

iphone - 在 iOS 上,如何使 cell.imageView 刷新其内容?

iphone - 如何在单击 iPhone 中的 UIButton 时打开相机?

xcode - 安装 iOS 4.2 和 4.2 SDK 后无法在设备上运行