FROM : Gerd Knops
DATE : Mon Mar 24 23:27:47 2008
> 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
DATE : Mon Mar 24 23:27:47 2008
> 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
| Related mails | Author | Date |
|---|---|---|
| Lincoln Green | Mar 24, 17:02 | |
| Cathy Shive | Mar 24, 17:06 | |
| Sherm Pendley | Mar 24, 17:11 | |
| Jonathan Hess | Mar 24, 20:37 | |
| Bertil Holmberg | Mar 24, 22:39 | |
| Gerd Knops | Mar 24, 23:27 | |
| Steve Weller | Mar 25, 01:58 | |
| Lincoln Green | Mar 28, 19:27 | |
| Steve Weller | Mar 29, 16:31 |






Cocoa mail archive

