0

I have an NSSearchField control where I want to show a few categories that are to appear as a menu when the user clicks on the arrow to the left. After reading Apple's documentation, I have gotten some idea. The following is my code.

// .h
@interface AppDelegate : NSObject {
    IBOutlet NSSearchField  *searchField;
}

// .m
- (void)awakeFromNib {    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidBecomeKey:) name:NSWindowDidBecomeKeyNotification object:[self window]];
    [window setContentBorderThickness:22.0 forEdge:NSMinYEdge];

    NSMenu *cellMenu = [[NSMenu alloc] initWithTitle:NSLocalizedString(@"Search Menu",@"Search Menu title")];
    NSMenuItem *item;
    item = [[NSMenuItem alloc] initWithTitle:@"Title" action:@selector(setSearchCategoryFrom:) keyEquivalent:@""];
    [item setTarget:self];
    [item setTag:1];
    [cellMenu insertItem:item atIndex:0];

    item = [[NSMenuItem alloc] initWithTitle:@"Username" action:@selector(setSearchCategoryFrom:) keyEquivalent:@""];
    [item setTarget:self];
    [item setTag:2];
    [cellMenu insertItem:item atIndex:1];
    id searchCell = [searchField cell];
    [searchCell setSearchMenuTemplate:cellMenu];
}

- (IBAction)setSearchCategoryFrom:(NSMenuItem *)menuItem {
    if ([menuItem tag] == 0) {

    }
    else {

    }
}

And the screenshot below shows the result. Now, I need to set the state of selection, whichever they choose, to 1 so that a checkmark will appear. How do I do that?

Thank you for your help.

enter image description here

El Tomato
  • 6,479
  • 6
  • 46
  • 75

2 Answers2

1

I would to add a categories menu (ex: search for Subject, Body or ...) in my NSSearchField. I've successfully set the menu but there is a problem if I try to set a menuitem to state:NSOffState. When I select a menu it should be to turn off the previous selected category. This is the code:

- (IBAction) menu_selectNewFilter:(id) sender {

NSMenuItem *m = [searchMenu itemWithTag: selectedFilter];
[m setState: NSOffState];
NSLog(@"Disabled %@ %d",[m title],[m tag]);

NSLog(@"Activate %@ %d",[sender title],[sender tag]);
[sender setState: NSOnState];

selectedFilter = [sender tag];

}

- (IBAction)setSearchCategoryFrom:(NSMenuItem *)menuItem {
    [[[sender menu] itemWithTag:lastSearchSelection] setState:NSOffState];
    [sender setState: NSOnState];
    lastSearchSelection = [sender tag];
}
Mayank Patel
  • 3,868
  • 10
  • 36
  • 59
0

The following should work.

// .h
@interface AppDelegate : NSObject {
    IBOutlet NSSearchField  *searchField;
    NSMenu *searchMenu;
}

// .m
@implementation AppDelegate {
    NSInteger lastSearchSelection;
}

- (void)awakeFromNib {            
    NSMenu *cellMenu = [[NSMenu alloc] initWithTitle:NSLocalizedString(@"Search Menu",@"Search Menu title")];
    NSMenuItem *item;
    item = [[NSMenuItem alloc] initWithTitle:@"Title" action:@selector(setSearchCategoryFrom:) keyEquivalent:@""];
    [item setTarget:self];
    [item setTag:1];
    [cellMenu insertItem:item atIndex:0];

    item = [[NSMenuItem alloc] initWithTitle:@"Username" action:@selector(setSearchCategoryFrom:) keyEquivalent:@""];
    [item setTarget:self];
    [item setTag:2];
    [cellMenu insertItem:item atIndex:1];
    id searchCell = [searchField cell];
    [searchCell setSearchMenuTemplate:cellMenu];
}

- (IBAction)setSearchCategoryFrom:(NSMenuItem *)menuItem {
    [[[sender menu] itemWithTag:lastSearchSelection] setState:NSOffState];
    [sender setState: NSOnState];
    lastSearchSelection = [sender tag];
}
El Tomato
  • 6,479
  • 6
  • 46
  • 75