在iOS 6中,不推荐使用viewWillUnload
和viewDidUnload
,并且UIViewControllers不再卸载在内存警告期间在屏幕上看不到的视图。 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;
}
}
代码示例下面是以下注释:
下次访问view属性时,将重新加载视图 就像第一次一样。
这里有一个明显的缺陷。如果未加载其视图的视图控制器收到内存警告,它将在行if ([self.view window] == nil)
中加载其视图,然后继续清理并再次释放它。充其量,这是低效的。在最坏的情况下,如果加载了复杂的视图层次结构和支持数据,则会使内存条件变得更糟。我在iOS模拟器中验证了这种行为。
我当然可以编写代码,但Apple文档出现这样的错误似乎很奇怪。我错过了什么吗?
答案 0 :(得分:17)
在视图控制器中正确检查正在加载的视图并在屏幕上显示:
if ([self isViewLoaded] && [self.view window] == nil)
我在iOS 6中的完整解决方案让视图控制器卸载视图并清理类似于iOS 5的内容如下:
// will not be called in iOS 6, see iOS docs
- (void)viewWillUnload
{
[super viewWillUnload];
[self my_viewWillUnload];
}
// will not be called in iOS 6, see iOS docs
- (void)viewDidUnload
{
[super viewDidUnload];
[self my_viewDidUnload];
}
// in iOS 6, view is no longer unloaded so do it manually
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
if ([self isViewLoaded] && [self.view window] == nil) {
[self my_viewWillUnload];
self.view = nil;
[self my_viewDidUnload];
}
}
- (void)my_viewWillUnload
{
// prepare to unload view
}
- (void)my_viewDidUnload
{
// the view is unloaded, clean up as normal
}