Re: Cant set state of NSMenuItems attached to NSSearchField
Re: Cant set state of NSMenuItems attached to NSSearchField
- Subject: Re: Cant set state of NSMenuItems attached to NSSearchField
- From: Jim Correia <email@hidden>
- Date: Sun, 30 Nov 2008 14:13:36 -0500
On Nov 30, 2008, at 2:03 PM, Mike Chambers wrote:
I have an NSMenu attached to an NSSearchField as a searchMenuTemplate.
[snip]
If the user selects "All", I want to remove all check marks from other
selected menu items. If the user selects anything except "All", then I
want to remove the check from the All menu item. In order to allow
this, I have outlets set for each menu item so I can change its state:
----------
IBOutlet NSMenuItem *allMenuItem;
IBOutlet NSMenuItem *startMenuItem;
IBOutlet NSMenuItem *endMenuItem;
IBOutlet NSMenuItem *countryMenuItem;
IBOutlet NSMenuItem *stationMenuItem;
IBOutlet NSMenuItem *notesMenuItem;
IBOutlet NSMenuItem *frequenciesMenuItem;
----------
First, some comments.
If you find yourself creating an outlet for every sub-element of a
user interface element, there is usually a better way to do what you
want to do. (For example, by having an outlet to the parent interface
element, and iterating the sub-elements, filtering by tag if necessary.)
Also, look at the header comment for NSSearchFieldCell's
searchMenuTemplate. This is just a template, not the actual menu used.
When I've done this in the past, I've used -validateMenuItem: to
adjust the state checkmark on the items in my search field menu.
- (BOOL)validateMenuItem:(NSMenuItem *)item
{
SEL action = [item action];
if (action == @selector(changeSearchFilterIdentifier:)) {
NSString *searchFilterIdentifier = [item representedObject];
[item setState: [searchFilterIdentifier isEqual: [self
searchFilterIdentifier]]];
return [self isSearchFilterIdentifierEnabled: searchFilterIdentifier];
}
return YES;
}
If you aren't going to go this route, you'll at least need to make
sure you are manipulating the actual menu, not the template. You can
ask the menu item in your action for its parent menu.
Jim
_______________________________________________
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