NSPopUpButton update
NSPopUpButton update
- Subject: NSPopUpButton update
- From: ALEXander <email@hidden>
- Date: Tue, 28 Apr 2009 12:53:05 +0200
Hello,
i use an NSPopUpButton to display color choices that the user can set
in user defaults. There is one view with an NSCollectionView
controlled by an NSDictionarycontroller, which populates the
dictionary CTColorsDictionary in user defaults with color values
(using NSKeyedArchiver) and a corresponding key (@"Red", @"Green", ...).
I then use an NSPopUpButton in another place to let the user choose
the color. The popupbutton is bound to another NSDictionaryController
which is bound to a shared user defaults controller pointing to
values.CTColorsdictionary.
To get a small image of the color into the menu I subclassed
NSPopUpButton and added these two methods to its implementation:
- (void) awakeFromNib
{
[[self menu] setDelegate:self];
[self menuNeedsUpdate:[self menu]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(menuNeedsUpdate:)
name:NSUserDefaultsDidChangeNotification object:nil];
}
- (void)menuNeedsUpdate:(NSMenu *)menu
{
NSDictionary* colors = [[NSUserDefaults standardUserDefaults]
dictionaryForKey:@"CTColorsDictionary"];
NSImage* image;
for (NSMenuItem* item in [self itemArray])
{
image = [[[NSImage alloc] initWithSize:NSMakeSize(20, 13)]
autorelease];
[image lockFocus];
NSColor* color = [NSKeyedUnarchiver unarchiveObjectWithData:[colors
valueForKey:[item title]]];
[color set];
NSRectFill(NSMakeRect(0, 0, 20, 13));
//[image setBackgroundColor:[NSColor blueColor]];
[image unlockFocus];
[item setImage:image];
}
[self synchronizeTitleAndSelectedItem];
[self display];
}
This works normally. The problem is now: When I change a color in user
defaults, I can see (for a tenth of a second or so) that the color
image of the currently selected item in the popup is redrawn, but then
the title gets overwritten by another method, which ignores the image
setting of my menu items and just displays the text.
Does anybody have an idea which method is doing this, and how I could
override it?
When I click once on the popupbutton everything is ok again and the
color image reappears.
ALEXander.
_______________________________________________
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