I am just trying to work out how the viewContro开发者_C百科ller is working in a simple iPhone app. My question is I am trying to see when the functions below get called, I have put NSLog commands in there to print to the console, but I don't see any of the below printing either when running or exiting my app, do they get called, should I see anything?
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
NSLog(@"-1-");
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
NSLog(@"-2-");
self.statusText = nil;
}
- (void)dealloc {
NSLog(@"-3-");
[statusText release];
[super dealloc];
}
EDIT_001:
-1- didReceiveMemoryWarning (as noted by Kenny) works via Hardware>Simulate Memory Warning
-2- I can see why this one is not working now, thank you.
-3- dealloc, I am quiting the running app using the white square at the bottom, does this do a full quit where I should see the NSLog from dealloc?
gary
to answer your question:
(void)didReceiveMemoryWarning won't fire while running just a simple application. You can test it by loading a bunch of retained objects (like images).
(void)viewDidUnload : as stated in the comment it only fires if you've got another view attached to your main view.
(void)dealloc : well, here you should see something. don't know why. did you check the Console of XCode?!
- (void)didReceiveMemoryWarning
is sent to the view controller when the application receives a memory warning (your app gets low on RAM). You can simulate it by selecting Hardware->Simulate Memory Warning in the Simulator.
- (void)viewDidUnload
is called when the controller’s view is released from memory. It is called during low-memory conditions when the view controller needs to release its view.
- (void)dealloc
is sent after the object is released to free the memory it occupies.
I hope this helps.
精彩评论