iOS6取消viewDidUnload的原因和解决方案

一直以来我们会在viewDidUnload方法中做一些清空outlet或者移除observer的事情。在viewDidUnload中清理observer其实并不是很安全,因此在iOS5中Apple引入了viewWillUnload,建议开发者们在viewWillUnload的时候就移除observer。而对于出现内存警告时,某些不用的view将被清理,这时候将自动意外执行viewWillUnload和viewDidUnload,很可能造成莫名其妙的crash,而这种内存警告造成的问题又因为其随机性难以debug。

于是Apple这次做了一个惊人的决定,直接在iOS6里把viewWillUnload和viewDidUnload标注为了Deprecated,并且不再会调用他们。绝大部分开发者其实是对iOS3.0以来就伴随我们的viewDidUnload是有深深的感情的,但是现在需要和这个方法说再见了。

官方ViewController programming guide 上说,在iOS6以后要这样干:

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

原文地址:https://www.cnblogs.com/yy3026906/p/2966703.html