| 关键词: nbsp View 内存 didReceiveMemory 控制器 Warning 方法 viewDidUnload 代码 OSMemoryNotifica |
内存警告 复制代码代码如下: typedef enum { OSMemoryNotificationLevelAny = -1, OSMemoryNotificationLevelNormal = 0, OSMemoryNotificationLevelWarning = 1, OSMemoryNotificationLevelUrgent = 2, OSMemoryNotificationLevelCritical = 3 }OSMemoryNotificationLevel;(5.0以后废弃了) 1、Warning (not-normal) — 退出或者关闭一些不必要的后台程序 e.g. Mail 响应内存警告: View Controller 生成view: 复制代码代码如下: -(void)didReceiveMemoryWarning { //In earlier versions of iOS, the system automatically attempts to unload a view controller's views when memory is low [super didReceiveMemoryWarning]; //didReceiveMemoryWarining 会判断当前ViewController的view是否显示在window上,如果没有显示在window上,则didReceiveMemoryWarining 会自动将viewcontroller 的view以及其所有子view全部销毁,然后调用viewcontroller的viewdidunload方法。
On iOS 6 and Later 1 系统发出警告或者ViewController本身调用导致didReceiveMemoryWarning被调用 2 - (void)didReceiveMemoryWarning;中释放当前不在使用的资源 ios6.0 LeaksDemo 复制代码代码如下: -(void)didReceiveMemoryWarning { [super didReceiveMemoryWarning];//即使没有显示在window上,也不会自动的将self.view释放。注意跟ios6.0之前的区分 // Add code to clean up any of your own resources that are no longer necessary. // 此处做兼容处理需要加上ios6.0的宏开关,保证是在6.0下使用的,6.0以前屏蔽以下代码,否则会在下面使用self.view时自动加载viewDidUnLoad if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) { //需要注意的是self.isViewLoaded是必不可少的,其他方式访问视图会导致它加载,在WWDC视频也忽视这一点。 if (self.isViewLoaded && !self.view.window)// 是否是正在使用的视图 { // 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;// 目的是再次进入时能够重新加载调用viewDidLoad函数。 } } } 内存不足时的处理 1.当控制器接收到内存警告时,会调用 didReceiveMemoryWarning 方法 复制代码代码如下: if (self.view.superview == nil) { // 检测控制器的view在不在屏幕上 // 就会尝试销毁控制器的view // 即将销毁的时候,就会调用控制器的 viewWillUnload // 销毁完毕的时候,就会调用控制器的 viewDidUnload方法 } else { // 不销毁控制器的view 3.当需要再次使用控制器的view时,又会调用loadView方法来创建view 4.接着会调用一系列的生命周期方法 5.生命周期循环 所以当我们的程序内存过大时,我们挂载在后台的QQ有时候会出现已经推出的情况!当我们再次点击的时候,QQ又重新加载运行起来! |
|
声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系
[邮箱地址] 删除
|