zoukankan      html  css  js  c++  java
  • iOS开发那些事-响应内存警告

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

    1、应用程序委托对象

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

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


    2、视图控制器

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

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


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

    3、其它类

    在其它类中可以使用通知,在内存警告时候iOS系统会发出UIApplicationDidReceiveMemoryWarningNotification通知,凡是在通知中心注册了

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

    我们在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调用

  • 相关阅读:
    CodeForces 288A Polo the Penguin and Strings (水题)
    CodeForces 289B Polo the Penguin and Matrix (数学,中位数)
    CodeForces 289A Polo the Penguin and Segments (水题)
    CodeForces 540C Ice Cave (BFS)
    网站后台模板
    雅图CAD
    mbps
    WCF学习-协议绑定
    数据库建表经验总结
    资源位置
  • 原文地址:https://www.cnblogs.com/zsw-1993/p/4880019.html
Copyright © 2011-2022 走看看