IOS 6下的viewDidUnload处理

ios6开始在内存紧张的情况下, 非活动界面也不会调用viewDidUnload了, 因些不会通过该方法释放出更多的可用内存。

为保持我们原代码风格不变,可定一个新的BaseViewController他继承自UIViewController, 在.m文件中重写didReceiveMemoryWarning方法,如下所示:


- (void)didReceiveMemoryWarning{
    float version = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (version >= 6.0){
        if ([self isViewLoaded] && [self.view window] == nil) {
            [self viewDidUnload];
            self.view = nil;
        }
    }

    [super didReceiveMemoryWarning];
}

然后在所有工程中原来继承自UIViewController的地方,全部改为继承自BaseViewController,并且不要再次重写didReceiveMemoryWarning方法,  这样viewDidUnload就可以像在ios 5中一样的调用效果了。



Memory Management

Memory is a critical resource in iOS, and view controllers provide built-in support for reducing their memory footprint at critical times. The UIViewController class provides some automatic handling of low-memory conditions through itsdidReceiveMemoryWarning method, which releases unneeded memory.

Prior to iOS 6, when a low-memory warning occurred, the UIViewController class purged its views if it knew it could reload or recreate them again later. If this happens, it also calls the viewWillUnload and viewDidUnload methods to give your code a chance to relinquish ownership of any objects that are associated with your view hierarchy, including objects loaded from the nib file, objects created in your viewDidLoad method, and objects created lazily at runtime and added to the view hierarchy. On iOS 6, views are never purged and these methods are never called. If your view controller needs to perform specific tasks when memory is low, it should override the didReceiveMemoryWarning method.

你可能感兴趣的:(IOS 6下的viewDidUnload处理)