Re: NSPopupButton menu and bindings
Re: NSPopupButton menu and bindings
- Subject: Re: NSPopupButton menu and bindings
- From: Matt Neuburg <email@hidden>
- Date: Tue, 01 Aug 2006 16:27:12 -0700
- Thread-topic: NSPopupButton menu and bindings
> Re: NSPopupButton menu and bindings
> FROM : Matt Neuburg
> DATE : Sat Jul 22 04:32:25 2006
>
> On Fri, 21 Jul 2006 20:05:27 -0500, Ashley Clark <<email_removed>> said:
>> I have a dictionary of objects and an array of keys that I want to place in
>> an NSPopupButton's menu.
>>
>> I have created countOfMenuItems and objectInMenuItemsAtIndex: methods to
>> return the objects in the dictionary based on my array of keys and this
>> works.
>>
>> What I'd like to do is be able to say that when the key is an empty string
>> (@"") to have the menu draw a separator. Is this possible with bindings?
>>
> I have never seen convincing instructions for causing a bound NSPopupButton's
> menu to display a separatorItem. This might make a good feature request. m.
Okay, I may have solved the problem.
Create an NSMenu subclass. Let's call it MyMenu. In IB, drag MyMenu.h into
the nib, and set the class of the menu of the NSPopupButton to be MyMenu.
(You will probably have to use the hierarchical instance list display rather
than the visual design window in order to do this.)
Here is the code for MyMenu:
- (id <NSMenuItem>)addItemWithTitle:(NSString *)aString
action:(SEL)aSelector
keyEquivalent:(NSString *)keyEquiv {
if ([aString isEqual: @"-"]) {
id <NSMenuItem> sep = [NSMenuItem separatorItem];
[self addItem:sep];
return sep;
}
return [super addItemWithTitle:aString action:aSelector
keyEquivalent:keyEquiv];
}
Now, when you supply the data that populates the menu, whatever key it is
that you're using for each item's title for the popup menu, give that key a
value of @"-" for the item in question. It will appear as a menu item
separator in the NSPopupButton.
The idea here is that even though there is a binding, there is no magic; the
menu must put on its pants on leg at a time. In particular, the way the menu
gets populated is by calling addItemWithTitle:... So we intercept this call
and supply a separatorItem just in case the requested title is @"-".
It seems to be working in my app, but YMMV. It would be interesting to hear
reports from others.
m.
--
matt neuburg, phd = email@hidden, http://www.tidbits.com/matt/
pantes anthropoi tou eidenai oregontai phusei
AppleScript: the Definitive Guide - Second Edition!
http://www.amazon.com/gp/product/0596102119
Take Control of Word 2004, Tiger, and more -
http://www.takecontrolbooks.com/tiger-customizing.html
Subscribe to TidBITS! It's free and smart. http://www.tidbits.com/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden