在内存警告(Apple doc flaw)中卸载iOS 6中的视图的正确方法是什么? [英] What is the proper way to unload views in iOS 6 in a memory warning (Apple doc flaw)?

查看:109
本文介绍了在内存警告(Apple doc flaw)中卸载iOS 6中的视图的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS 6中,不推荐使用 viewWillUnload viewDidUnload ,UIViewControllers不再卸载屏幕上不可见的视图在记忆警告期间。 查看控制器编程指南有一个如何手动恢复此行为的示例。

In iOS 6, viewWillUnload and viewDidUnload are deprecated and UIViewControllers no longer unload views that are not visible on screen during a memory warning. The View Controller Programming Guide has an example of how to manually restore this behavior.

以下是代码示例:

- (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;
    }
}

代码示例下面是以下注释:

Below the code sample is the following note:


下次访问view属性时,视图将重新加载$ ​​b $ b,与第一次完全一样。

The next time the view property is accessed, the view is reloaded exactly as it was the first time.

这里有一个明显的缺陷。如果未加载其视图的视图控制器收到内存警告,它将在行中加载其视图if([self.view window] == nil)然后继续清理并再次释放它。充其量,这是低效的。在最坏的情况下,如果加载了复杂的视图层次结构和支持数据,则会使内存条件变得更糟。我在iOS模拟器中验证了这种行为。

There is an obvious flaw here. If a view controller that has not loaded its view receives a memory warning it will load its view in the line if ([self.view window] == nil) and then proceed to clean up and release it again. At best, this is inefficient. At worst, it makes the memory conditions worse if a complex view hierarchy and supporting data are loaded. I verified this behavior in the iOS simulator.

我当然可以编写代码,但Apple文档出现这样的错误似乎很奇怪。我错过了什么?

I can certainly code around this but seems odd for Apple docs to have such an error. Am I missing something?

推荐答案

在视图控制器中正确检查正在加载的视图并在屏幕上显示:

The correct check in a view controller for the view being loaded and on screen is:

if([self isViewLoaded]&& [self.view window] == nil)

我在iOS 6中的完整解决方案让视图控制器卸载视图和类似于iOS 5的清理如下:

My full solution in iOS 6 to have a view controller unload views and cleanup similar to iOS 5 is the following:

// 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
}

这篇关于在内存警告(Apple doc flaw)中卸载iOS 6中的视图的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆