二、当应用接收到内存警告时应该怎么处理
2015-07-01 19:01
316 查看
当收到内存警告的时候,应用程序会将警告一级一级往下传递,传递顺序是UIApplication->UIWindow->rootViewController(如果有子控制器)->childViewControllers。
当控制器接收到警告之后,就会调用didReceiveMemoryWarning方法。一般会在这个方法中做几件事:
1、在iOS6之前的处理方式(见图)
2、在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;
}
}
1> 调用父类的didReceiveMemoryWarning来确保父类的行为能够被执行。
2> 清理控制器不再需要的资源
3> 判断控制器的view是不是正在在窗口上显示,如果不是,先保存跟view或子view相关的数据。清空所有子控件的强引用。
4> 最后设置控制器的view为nil。
当控制器接收到警告之后,就会调用didReceiveMemoryWarning方法。一般会在这个方法中做几件事:
1、在iOS6之前的处理方式(见图)
2、在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;
}
}
1> 调用父类的didReceiveMemoryWarning来确保父类的行为能够被执行。
2> 清理控制器不再需要的资源
3> 判断控制器的view是不是正在在窗口上显示,如果不是,先保存跟view或子view相关的数据。清空所有子控件的强引用。
4> 最后设置控制器的view为nil。
相关文章推荐
- 前端规范
- 最初,世上有许多令狐冲
- bzoj1017 [JSOI2008]魔兽地图DotR
- 移动端的常见问题
- 使用c3p0与DBCP连接池,造成的MySql 8小时问题解决方案
- hdu 1443
- 在Linux上,编写一个每秒接收100万UDP数据包的程序究竟有多难
- python3.4学习笔记(十一) 列表、数组实例
- SQL2008R2日志传送需要注意点
- 如此繁荣的移动webapp开发市场:总结当下的一些移动web开发套件
- Linux上安装完mysql需要注意的事项
- meta你到底了解多少
- 在storyboard里设置VC的 tab bar item 的 selected image 运行tabbar不显示
- SEAndroid架构
- 学习C#和SQL的书籍
- HTTP、TCP/IP、socket及bluetooth-socket的整理
- Contains Duplicate II
- 使用AmplifyJS和JQuery编写更好更优雅的javascript事件处理代码
- 自动寻路方案
- Cookie的用法