Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
516 CHAPTER 14: Hey! You! Get onto iCloud! While we're on the subject of dealing with color choices, scroll down to the bottom and fill in the implementation for the chooseColor: method: - (IBAction)chooseColor:(id)sender { NSInteger selectedColorIndex = [(UISegmentedControl *)sender selectedSegmentIndex]; NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; [prefs setInteger:selectedColorIndex forKey:@"selectedColorIndex"]; } We're nearly finished with this class, but we need to make one more change. Remember when we mentioned the autosaving that takes place when a document is notified that some editing has occurred, triggered by registering an undoable action? The save normally happens within about ten seconds after the edit occurs. Like the other saving and loading procedures we described earlier in this chapter, it happens in a background thread, so that normally the user won't even notice. However, that works only as long as the document is still around. With our current setup, there's a risk that when the user hits the back button to go back to the master list, the document instance will be deallocated without any save operation occurring, and the user's latest changes will be lost. To make sure this doesn't happen, we need to add some code to the viewWillDisappear: method to close the document as soon as the user navigates away from the detail view. Closing a document causes it to be automatically saved, and again, the saving occurs on a background thread. In this particular case, we don't need to do anything when the save is done, so we pass in nil instead of a block. Make this change to viewWillDisappear::