Is there a way to programmatically connect to a NIB file and read it's
outlets without changing my File's Owner? I have a color well in one
NIB (My Prefs NIB) and a window in another. I want the color well to
change the window's background. I have an action called setColor:, but
I cannot figure out how to access the window outlet from the NIB
containing the window. Please help!


Given you are talking preferences, you probably do not want a direct connection at all but rather use the user defaults system as middle man, which has the additional benefit that it will save the color for you:

In IB select the Bindings info for your color well, and bind the value as follows:

Bind to: Shared Defaults
Model Key Path: myWindowBackgroundColor (or whatever)
Value Transformer: NSUnachiveFromData

Now somewhere in your application you probably want to set an initial default, like so:

+ (void)initialize {
        
        //
        // Register initial defaults
        //
        NSMutableDictionary *defaults=[NSMutableDictionary dictionary];
        
[defaults setObject:[NSArchiver archivedDataWithRootObject:[NSColor windowBackgroundColor]] forKey:@"myWindowBackgroundColor"];
        // Set other initial defaults here
        
        [[NSUserDefaults standardUserDefaults]registerDefaults:defaults];
}


Now in your window or window controller class you want to get notified when that color is changed. So somewhere in -init or -awakeFromNib add this:

[[NSUserDefaultsController sharedUserDefaultsController]addObserver:self
                forKeyPath:@"values.myWindowBackgroundColor"
                options:0
                context:nil];   

In the same class you need to implement an observer method like so:

- (void)observeValueForKeyPath:(NSString*)keyPath
                      ofObject:(id)object
                        change:(NSDictionary*)change
                       context:(void*)context
{
        if([keyPath isEqualToString:@"values.myWindowBackgroundColor"])
        {
                [self setBackgroundColorFromDefaults];
        }
}


And finally the method that sets the color:

- (void)setBackgroundColorFromDefaults {
        
        NSUserDefaults  *ud=[NSUserDefaults standardUserDefaults];
NSColor *bgColor=[NSUnarchiver unarchiveObjectWithData:[ud dataForKey:@"myWindowBackgroundColor"]];
        
        [self setColor:bgColor];
}

As last step you should probably call setBackgroundColorFromDefaults somewhere early in your code so that your window start out with the right color.

Alternatively don't set the color at all, but have the observer trigger a redraw, and in the drawing code read the color from the defaults.

Hope that helps!

Gerd


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to