FROM : Steve Weller
DATE : Tue Mar 25 01:58:51 2008
On Mar 24, 2008, at 3:27 PM, Gerd Knops wrote:
>> 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!
I use a different approach -- it may not be the "right" one, but it
works for me. In my case I have a gray slider in a prefs panel. In IB
I bind that to Shared User Defaults and provide Controller Key
"values" and Model Key Path "browserBackgroundGray". That deals with
everything except updating the browser view.
In the prefs pane window controller I do this:
- (IBAction)browserBackgroundGrayAction:(id)sender;
{
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center postNotificationName:@"browserBackgroundColorChanged"
object:self];
}
In the browser view controller I do this:
// Observe changes in browser background color
-(void)startObservingChanges;
{
// Register to observe notifications of background color changes
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self
selector:@selector(browserBackgroundColorChange:)
name:@"browserBackgroundColorChanged" object:nil];
}
-(void)stopObservingChanges;
{
//
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center removeObserver:self];
}
These are called by appropriate methods that the window controller
makes.
I implement the color change like this:
// Handle browser background color notification
-(void)browserBackgroundColorChange:(NSNotification*)note;
{
float grayValue;
NSColor *color;
grayValue = [[NSUserDefaults standardUserDefaults]
floatForKey:@"browserBackgroundGray"];
color = [NSColor colorWithCalibratedRed:grayValue green:grayValue
blue:grayValue alpha:1.0];
[browserView setValue:color forKey:IKImageBrowserBackgroundColorKey];
[browserView setNeedsDisplay:YES];
}
In my multiple document application this changes the background of the
browser in all documents at once.
>
> 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 (<email_removed>)
>
> 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/<email_removed>
>
> This email sent to <email_removed>
--
Blog: Photos: <A href="http://bagelturf.smugmug.com/">http://bagelturf.smugmug.com/
DATE : Tue Mar 25 01:58:51 2008
On Mar 24, 2008, at 3:27 PM, Gerd Knops wrote:
>> 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!
I use a different approach -- it may not be the "right" one, but it
works for me. In my case I have a gray slider in a prefs panel. In IB
I bind that to Shared User Defaults and provide Controller Key
"values" and Model Key Path "browserBackgroundGray". That deals with
everything except updating the browser view.
In the prefs pane window controller I do this:
- (IBAction)browserBackgroundGrayAction:(id)sender;
{
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center postNotificationName:@"browserBackgroundColorChanged"
object:self];
}
In the browser view controller I do this:
// Observe changes in browser background color
-(void)startObservingChanges;
{
// Register to observe notifications of background color changes
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self
selector:@selector(browserBackgroundColorChange:)
name:@"browserBackgroundColorChanged" object:nil];
}
-(void)stopObservingChanges;
{
//
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center removeObserver:self];
}
These are called by appropriate methods that the window controller
makes.
I implement the color change like this:
// Handle browser background color notification
-(void)browserBackgroundColorChange:(NSNotification*)note;
{
float grayValue;
NSColor *color;
grayValue = [[NSUserDefaults standardUserDefaults]
floatForKey:@"browserBackgroundGray"];
color = [NSColor colorWithCalibratedRed:grayValue green:grayValue
blue:grayValue alpha:1.0];
[browserView setValue:color forKey:IKImageBrowserBackgroundColorKey];
[browserView setNeedsDisplay:YES];
}
In my multiple document application this changes the background of the
browser in all documents at once.
>
> 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 (<email_removed>)
>
> 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/<email_removed>
>
> This email sent to <email_removed>
--
Blog: Photos: <A href="http://bagelturf.smugmug.com/">http://bagelturf.smugmug.com/
| 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

