问题
I have put NSLogs in all my classes including my UIApplicationDelegate subclass. I am curious - and a bit nervous - about why I am not seeing them echo anything when I press the home button. I am running in the XCode simulator.
Since iPhone/iPad runs a single app at a time, doesn't hitting the home button discard all traces of the running app?
Thanks,
Doug
回答1:
When an app is terminated, its memory is simply freed. Dealloc is not called, it does not pass go or collect $200. This is normal and intended.
回答2:
Chuck is correct, the dealloc's don't matter at that point. If you want to do something as your app is expiring, implement this in your app delegate class:
- (void)applicationWillTerminate:(UIApplication *)application {
// goodbye...
}
回答3:
You should implement -(void)applicationWillTerminate:(UIApplication*)application{ } this method will get called when user hit home button.
回答4:
You might also want to think about the additional multitasking support that has been announced. Obviously the details are still under NDA, but pushing Home does not necessarily stop your app.
And think about the various circumstances where your app may already be terminated quickly (e.g. if a call comes in and the system runs out of RAM). Do your cleanup with the application lifecycle messages.
来源:https://stackoverflow.com/questions/2679847/iphone-shouldnt-hitting-the-home-button-cause-uiapplicationdelegates-dealloc