Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > NSSearchField Menu Weirdness

NSSearchField Menu Weirdness
Thread Tools
iOliverC
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
May 31, 2004, 09:14 AM
 
Hi guys,

I have a NSSearchField which has a menu attached to it. I attach the menu via this code:

- (void)setupSearchBar
{
NSMenu *cellMenu = [[[NSMenu alloc] initWithTitle:@"Search Menu"] autorelease];
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"PHP.Net Manual" action:@selector(setPHP:) keyEquivalent:@""];
NSMenuItem *item2 = [[NSMenuItem alloc] initWithTitle:@"MySQL Manual" action:@selector(setMySQL:) keyEquivalent:@""];
id searchCell = [searchBar cell];
[cellMenu insertItem:item atIndex:0];
[cellMenu insertItem:item2 atIndex:1];
[item release];
[item2 release];
[searchCell setSearchMenuTemplate:cellMenu];
}

Now, the menu is attached fine, but the menu items are not enabled, they are just grey. I had a look in Apple's docs and this is how they do it too. In case its needed, here are the methods I use (setPHP and setMySQL):

- (void)setPHP:(id)sender
{
id searchBarCell = [searchBar cell];
[searchBarCell setPlaceholderString:@"PHP.Net Manual"];
[phpMenuItem setState:NSOnState];
[sqlMenuItem setState:NSOffState];
[[NSUserDefaults standardUserDefaults] setObject:@"PHP.Net Manual" forKey:@"HelpSearchMode"];
}

- (void)setMySQL:(id)sender
{
id searchBarCell = [searchBar cell];
[searchBarCell setPlaceholderString:@"MySQL Manual"];
[sqlMenuItem setState:NSOnState];
[phpMenuItem setState:NSOffState];
[[NSUserDefaults standardUserDefaults] setObject:@"MySQL Manual" forKey:@"HelpSearchMode"];
}

Now, you would have thought that it would have worked, but it won't! I had a look in the cocoa-dev archives, and someone else had a similar problem but got no reply.

Any ideas?

Thanks,
Oliver
     
Rickster
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status: Offline
Reply With Quote
May 31, 2004, 05:49 PM
 
By setting an action but not a target on the two menu items, you're implicitly setting their target to "first responder". Menu items are automatically validated -- when you pull up the menu, it searches the responder chain for an object that responds to setPHP:, and then disables the menu item when it doesn't find one. (And does the same for the other menu item.) Is the object you've implemented those action methods on in the responder chain? If not, you need to either insert it there or set it as an explicit target on the menu items.
Rick Roe
icons.cx | weblog
     
iOliverC  (op)
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
May 31, 2004, 06:03 PM
 
Turns out I needed a setTarget:self on each menu item .

Thanks,
Oliver
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 03:24 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,