Challenge + question


#1

Here was my simple solution to the challenge:

[code]- (IBAction)resetDefults:(id)sender {
[[NSUserDefaults standardUserDefaults] removeObjectForKey:BNREmptyDocKey];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:BNRTableBgColorKey];
[self windowDidLoad];
}

[/code]

This gets called from the button, with nothing else required.

My question: would this be considered valid practice to call the [self windowDidLoad] method like this, or should I abstract out the setting of the colorWell and checkbox to a different method which is called by both the windowDidLoad and resetDefaults methods?


#2

My solution looked very similar

- (IBAction)resetPreferences:(id)sender { NSLog(@"Resetting Defaults"); [[NSUserDefaults standardUserDefaults] removeObjectForKey:BNREmptyDocKey]; [[NSUserDefaults standardUserDefaults] removeObjectForKey:BNRTableBgColorKey]; }

I didn’t call out to windowDidLoad though.


#3

dezinerau: Apologies for the late reply – somehow I missed your original post.

You should definitely abstract out those sorts of settings into their own method. It may work fine in this case, but it’s a bad habit to get into.

Adam


#4

I’m taking a different approach and re-writing the preference defaults

  • (IBAction)resetPreferences:(id)sender {
    // Create a dictionary
    NSMutableDictionary *defaultValues = [NSMutableDictionary dictionary];

    // Archive the color object
    NSData *colorAsData = [NSKeyedArchiver archivedDataWithRootObject:[NSColor yellowColor]];

    // Put the defaults in the dictionary
    [defaultValues setObject:colorAsData forKey:BNRTableBgColorKey];
    [defaultValues setObject:[NSNumber numberWithBool:YES] forKey:BNREmptyDocKey];

    // Register the dictionary of defaults
    [[NSUserDefaults standardUserDefaults]registerDefaults:defaultValues];
    NSLog(@“registered defaults: %@”, defaultValues);
    [self windowDidLoad];
    }

This works and redraws the Preference window (thanks dezinerau for windowDidLoad), however the RMDocument window is not getting updated. I notice that changing the table background color also has no effect on the open RMDocument table background color. It does, however, write the new preference out and reads it in on the next launch.

I’m trying to figure out how “changeBackgroundColor” and “resetDefaults” can update the current window with the new settings.

Thanks, – jmca


#5

jmca -

Take a look at the next chapter. You want to use notifications for this.

Cheers,
Bob


#6

Thanks Bob. I was one page away from the answer.