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