Re: NSColorWell and bindings
Re: NSColorWell and bindings
- Subject: Re: NSColorWell and bindings
- From: Jerry Krinock <email@hidden>
- Date: Wed, 10 Jun 2009 15:27:26 -0700
On 2009 Jun 10, at 13:16, Lorenzo Thurman wrote:
NSColor * color = [NSColor selectedMenuItemColor];
[defaults setObject:[NSArchiver archivedDataWithRootObject:color]
forKey:
@"textColor"];
NSData * colorData = [defaults objectForKey:@"textColor"];
// Other default values
[[NSUserDefaultsController sharedUserDefaultsController]
setInitialValues
:defaults];
There is a Data value in the preferences file, so there must be
something
else going on.
Storing a color in user defaults is something you'd expect to be easy
but is not. I wrote a little category that helps, but if I recall
correctly there are a few other hoops you need to jump through to make
it work with bindings. Anyhow they're all explained in the link to
apple.com given below. Read that whole web page and do what it says.
#import <Cocoa/Cocoa.h>
/*!
@brief
@details The NSColor methods were copied from
http://developer.apple.com/documentation/Cocoa/Conceptual/DrawColor/Tasks/StoringNSColorInDefaults.html
*/
@interface NSUserDefaults (MoreTypes)
- (void)setColor:(NSColor*)aColor
forKey:(NSString*)aKey ;
- (NSColor*)colorForKey:(NSString*)aKey ;
@end
@implementation NSUserDefaults (MoreTypes)
- (void)setColor:(NSColor *)aColor forKey:(NSString *)aKey {
NSData* theData = [NSArchiver archivedDataWithRootObject:aColor] ;
[self setObject:theData forKey:aKey] ;
}
- (NSColor*)colorForKey:(NSString *)aKey {
NSColor* theColor = nil ;
NSData* theData = [self dataForKey:aKey] ;
if (theData != nil) {
theColor = (NSColor*)[NSUnarchiver
unarchiveObjectWithData:theData] ;
}
return theColor ;
}
@end
_______________________________________________
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