码迷,mamicode.com
首页 > 其他好文 > 详细

当应用程序接收到内存警告的时候怎么处理?

时间:2016-05-29 23:04:46      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

当收到内存警告的时候,应用程序会将警告一级一级往下传递,传递顺序是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

 

技术分享

当应用程序接收到内存警告的时候怎么处理?

标签:

原文地址:http://www.cnblogs.com/isugus/p/5540533.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!