博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS开发那些事-响应内存警告
阅读量:7064 次
发布时间:2019-06-28

本文共 2096 字,大约阅读时间需要 6 分钟。

好的应用应该在系统内存警告情况下释放一些可以重新创建的资源。在iOS中我们可以在应用程序委托对象、视图控制器以及其它类中获得系统内存警告消息。

1、应用程序委托对象

在应用程序委托对象中接收内存警告消息,需要重写applicationDidReceiveMemoryWarning:方法。AppDelegate的代码片段:

 

 
  1. - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application 
  2.  
  3.  
  4. NSLog(@”AppDelegate中调用applicationDidReceiveMemoryWarning:”); 
  5.  

2、视图控制器

在视图控制器中接收内存警告消息,需要重写didReceiveMemoryWarning方法。ViewController的代码片段:

 

 
  1. - (void)didReceiveMemoryWarning 
  2.  
  3.  
  4. NSLog(@”ViewController中didReceiveMemoryWarning调用”); 
  5.  
  6. [super didReceiveMemoryWarning]; 
  7.  
  8. //释放成员变量 
  9.  
  10. [_listTeams release]; 
  11.  

注意释放资源代码应该放在[super didReceiveMemoryWarning]语句下面。

3、其它类

在其它类中可以使用通知,在内存警告时候iOS系统会发出 UIApplicationDidReceiveMemoryWarningNotification通知,凡是在通知中心注册了 UIApplicationDidReceiveMemoryWarningNotification通知的类都会接收到内存警告通知。 ViewController的代码片段:

 

 
  1. - (void)viewDidLoad 
  2.  
  3.  
  4. [super viewDidLoad]; 
  5.  
  6. NSBundle *bundle = [NSBundle mainBundle]; 
  7.  
  8. NSString *plistPath = [bundle pathForResource:@"team" 
  9.  
  10. ofType:@"plist"]; 
  11.  
  12. //获取属性列表文件中的全部数据 
  13.  
  14. NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath]; 
  15.  
  16. self.listTeams = array; 
  17.  
  18. [array release]; 
  19.  
  20.     //接收内存警告通知,调用handleMemoryWarning方法处理 
  21.  
  22.     NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; 
  23.  
  24.     [center addObserver:self 
  25.  
  26.                selector:@selector(handleMemoryWarning) 
  27.  
  28.                    name:UIApplicationDidReceiveMemoryWarningNotification 
  29.  
  30.                  object:nil]; 
  31.  
  32.  
  33. //处理内存警告 
  34.  
  35. -(void) handleMemoryWarning 
  36.  
  37.  
  38.     NSLog(@”ViewController中handleMemoryWarning调用“); 
  39.  

我们在viewDidLoad方法中注册UIApplicationDidReceiveMemoryWarningNotification消 息,接收到报警信息调用handleMemoryWarning方法。这些代码完全可以写在其它类中,在ViewController中重写 didReceiveMemoryWarning方法就可以了,本例这是示意性介绍一下 UIApplicationDidReceiveMemoryWarningNotification报警消息。

内存警告在设备上出现并不是经常的,一般我们没有办法模拟,但模拟器上有一个功能可以模拟内存警告,启动模拟器,选择模拟器菜单硬件→模拟内存警告,这个时候我们会在输出窗口中看到内存警告发生了。

2012-11-06 16:49:16.419 RespondMemoryWarningSample[38236:c07] Received memory warning.

2012-11-06 16:49:16.422 RespondMemoryWarningSample[38236:c07] AppDelegate中调用applicationDidReceiveMemoryWarning:

2012-11-06 16:49:16.422 RespondMemoryWarningSample[38236:c07] ViewController中handleMemoryWarning调用

2012-11-06 16:49:16.423 RespondMemoryWarningSample[38236:c07] ViewController中didReceiveMemoryWarning调用

转载地址:http://fgjll.baihongyu.com/

你可能感兴趣的文章
ATL接口返回类型&&ATL接口返回字符串BSTR*
查看>>
51 Nod 1008 N的阶乘 mod P【Java大数乱搞】
查看>>
空间统计之七:中心要素
查看>>
自己写的一部分斗地主的程序,没有去写界面,临时是用黑框来显示的
查看>>
nginx学习1
查看>>
mysql批量删除相同前缀的表格
查看>>
HDU 5358 First One(枚举)
查看>>
mac brew 安装 nginx fpm mysql 教程
查看>>
c27---typedef
查看>>
【】Python】异常处理try...except、raise
查看>>
GNU General Public License v3.0
查看>>
Django:(博客系统)添加文章(中文)出现UnicodeEncodeError乱码
查看>>
Win10 UWP开发:摄像头扫描二维码/一维码功能
查看>>
回声状态网络(ESN)基础教程
查看>>
windows 上rsync客户端使用方法
查看>>
curl 上传文件
查看>>
pcre和正则表达式的误点
查看>>
眼睛问题
查看>>
UWP项目生成安装包远程安装在树莓派上
查看>>
VUE -- ejs模板的书写
查看>>