Getting Started with iOS development: responding to memory warnings

Source: Internet
Author: User
Tags notification center

A good application should release some resources that can be recreated in the event of a system memory warning. In iOS we can get the system memory warning message in the application delegate object, the view controller, and other classes.

1. Application Delegate Object

To receive a memory warning message in an application delegate object, you need to override the Applicationdidreceivememorywarning: method. Code snippet for Appdelegate:

-(void) applicationdidreceivememorywarning: (uiapplication *) application 
     
{ 
     
NSLog (@) Call applicationdidreceivememorywarning in appdelegate: "); 
     
}

2. View Controller

To receive a memory warning message in the view controller, you need to override the Didreceivememorywarning method. Code snippet for Viewcontroller:

-(void) didreceivememorywarning 
     
{ 
     
NSLog (didreceivememorywarning call in @ "Viewcontroller"); 
     
[Super didreceivememorywarning]; 
     
Free member Variable 
     
[_listteams release]; 
     
}

Note the release of the resource code should be placed under the [Super didreceivememorywarning] statement.

3, other types

Notifications can be used in other classes, and the iOS system will issue uiapplicationdidreceivememorywarningnotification notifications when memory warnings are registered in the Notification center The UIAPPLICATIONDIDRECEIVEMEMORYWARNINGNOTIFICATION notification class receives a memory warning notification. Code snippet for Viewcontroller:

-(void) viewdidload {[Super viewdidload]; 
     
NSBundle *bundle = [NSBundle mainbundle]; 
     
NSString *plistpath = [Bundle pathforresource:@ "Team" oftype:@ "plist"]; 
     
Gets all the data in the property list file Nsarray *array = [[Nsarray alloc] initwithcontentsoffile:plistpath]; 
     
Self.listteams = array; 
     
    [Array release]; 
     
    Receive memory warning notification, invoke Handlememorywarning method to process nsnotificationcenter *center = [Nsnotificationcenter defaultcenter]; [Center addobserver:self selector: @selector (handlememorywarning) NA 
     
Me:uiapplicationdidreceivememorywarningnotification Object:nil]; 
     
}//Processing memory warning-(void) handlememorywarning {NSLog (handlememorywarning invocation in @ viewcontroller); }

We register the uiapplicationdidreceivememorywarningnotification message in the Viewdidload method and receive the alert information to call the Handlememorywarning method. This code can be written in other classes, rewrite the Didreceivememorywarning method in Viewcontroller, in this case it's a schematic introduction Uiapplicationdidreceivememorywarningnotification Alarm message.

Memory warning appears on the device is not often, generally we have no way to simulate, but the simulator has a function to simulate memory warning, start the simulator, select the simulator menu hardware → analog memory warning, this time we will see in the Output Window memory warning occurred.

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

Call applicationdidreceivememorywarning in 2012-11-06 16:49:16.422 respondmemorywarningsample[38236:c07] Appdelegate:

2012-11-06 16:49:16.422 respondmemorywarningsample[38236:c07] Viewcontroller handlememorywarning Call

2012-11-06 16:49:16.423 respondmemorywarningsample[38236:c07] Viewcontroller didreceivememorywarning Call

This article is from the "Dongsheng" blog, please be sure to keep this source http://2009315319.blog.51cto.com/701759/1133367

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.