Cocoa Programming for Mac OS X - Aaron Hillegass [70]
Letting the User Edit the Defaults
Next, you will alter the PreferenceController class so that the Preferences panel will cause the defaults database to get updated. Declare the following methods in PreferenceController.h:
+ (NSColor *)preferenceTableBgColor;
+ (void)setPreferenceTableBgColor:(NSColor *)color;
+ (BOOL)preferenceEmptyDoc;
+ (void)setPreferenceEmptyDoc:(BOOL)emptyDoc;
These class methods will make it easier for us to set and get the current default values, and will abstract away the details of how the preferences are stored.
Implement the new class methods in PreferenceController.m:
+ (NSColor *)preferenceTableBgColor
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *colorAsData = [defaults objectForKey:BNRTableBgColorKey];
return [NSKeyedUnarchiver unarchiveObjectWithData:colorAsData];
}
+ (void)setPreferenceTableBgColor:(NSColor *)color
{
NSData *colorAsData =
[NSKeyedArchiver archivedDataWithRootObject:color];
[[NSUserDefaults standardUserDefaults] setObject:colorAsData
forKey:BNRTableBgColorKey];
}
+ (BOOL)preferenceEmptyDoc
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
return [defaults boolForKey:BNREmptyDocKey];
}
+ (void)setPreferenceEmptyDoc:(BOOL)emptyDoc
{
[[NSUserDefaults standardUserDefaults] setBool:emptyDoc
forKey:BNREmptyDocKey];
}
Now let’s modify windowDidLoad and the action methods to make use of the preferences:
- (void)windowDidLoad
{
[super windowDidLoad];
[colorWell setColor:
[PreferenceController preferenceTableBgColor]];
[checkbox setState:
[PreferenceController preferenceEmptyDoc]];
}
- (IBAction)changeBackgroundColor:(id)sender
{
NSColor *color = [colorWell color];
[PreferenceController setPreferenceTableBgColor:color];
}
- (IBAction)changeNewEmptyDoc:(id)sender
{
NSInteger state = [checkbox state];
[PreferenceController setPreferenceEmptyDoc:state];
}
In the windowDidLoad method, you are reading the defaults and making the color well and check box reflect the current settings. In changeBackgroundColor: and changeNewEmptyDoc:, you are updating the defaults database.
You should now be able to build and run your application. It will read and write to the defaults database, so the Preferences panel will display the last color you chose and indicate whether the check box was on or off. You have not, however, done anything with this information yet, so the untitled document will continue to appear, and the background of the table view will continue to be white.
Using the Defaults
Now you are going to use the defaults. First, you will make your AppController become a delegate of the NSApplication object and suppress the creation of an untitled document, depending on the user defaults. Then, in RMDocument, you will set the background color of the table view from the user defaults.
Suppressing the Creation of Untitled Documents
As before, there are two steps to creating a delegate: implementing the delegate method and setting the delegate outlet to point to the object (Figure 13.4).
Figure 13.4. Delegate Suppresses Creation of Untitled Documents
Before automatically creating a new untitled document, the NSApplication object will send the message applicationShouldOpenUntitledFile: to its delegate. In AppController.m, add the following method:
- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender
{
NSLog(@"applicationShouldOpenUntitledFile:");
return [PreferenceController preferenceEmptyDoc];
}
To make your AppController the delegate of the NSApplication object,