applicationDidEnterBackground - How to save my user data
A novice's question.
I have one NotePageAppDelegate and one NotePageViewController. Simple. In my ViewController, I have defined a method to save the user data. It works perfectly if I click a button in my app which calls the method saveToDisk which is defined in my ViewController.
-(IBAction) saveToDisk
{ // saves the file to the disk
}
This works perfectly if I click my button. However, 开发者_高级运维if I close the app (i.e. in the Simulator clicking the Home Button), I would like to automatically save the page. I thought this is rather straight forward, so I put this in my AppDelegate:
- (void)applicationDidEnterBackground:(UIApplication *)application {
NotePageViewController* controller = [NotePageViewController alloc];
[controller saveToDisk];
[controller release];
}
However, this has no effect whatsoever. Have I forgotten to define something else? Or should this theoretically do the job if I press the Home Button in the Simulator?
Thanks for your help!
I'm surprised that doesn't crash, since you're calling a method on an uninitialized instance.
You have the right idea, but you should be calling saveToDisk
on an instance of the controller that already exists, not creating a new one that probably has no user modifications.
精彩评论