• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Communications between multiple NIB's
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Communications between multiple NIB's


  • Subject: Re: Communications between multiple NIB's
  • From: Steve Weller <email@hidden>
  • Date: Mon, 24 Mar 2008 17:58:51 -0700


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@hidden)

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:
This email sent to email@hidden

-- Blog: http://www.bagelturf.com/ Photos: http://bagelturf.smugmug.com/




_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:
This email sent to email@hidden


References: 
 >Re: Communications between multiple NIB's (From: Bertil Holmberg <email@hidden>)
 >Re: Communications between multiple NIB's (From: Gerd Knops <email@hidden>)

  • Prev by Date: NSTextStorage subclassing questions
  • Next by Date: Re: What is this invisible character?
  • Previous by thread: Re: Communications between multiple NIB's
  • Next by thread: Re: Communications between multiple NIB's
  • Index(es):
    • Date
    • Thread