【发布时间】:2012-09-14 12:10:09
【问题描述】:
在 iOS 6 中,viewWillUnload 和 viewDidUnload 已弃用,并且 UIViewController 在内存警告期间不再卸载屏幕上不可见的视图。 View Controller Programming Guide 有一个如何手动恢复此行为的示例。
这是代码示例:
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Add code to clean up any of your own resources that are no longer necessary.
if ([self.view window] == nil)
{
// Add code to preserve data stored in the views that might be
// needed later.
// Add code to clean up other strong references to the view in
// the view hierarchy.
self.view = nil;
}
}
代码示例下面是以下注释:
下次访问视图属性时,会重新加载视图 和第一次完全一样。
这里有一个明显的缺陷。如果尚未加载其视图的视图控制器收到内存警告,它将在if ([self.view window] == nil) 行中加载其视图,然后继续清理并再次释放它。充其量,这是低效的。在最坏的情况下,如果加载了复杂的视图层次结构和支持数据,它会使内存条件变得更糟。我在 iOS 模拟器中验证了这种行为。
我当然可以对此进行编码,但对于 Apple 文档来说,出现这样的错误似乎很奇怪。我错过了什么吗?
【问题讨论】:
标签: uiviewcontroller ios6 didreceivememorywarning