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

4

2 回答 2

1

我想在我的 NSSearchField 中添加一个类别菜单(例如:搜索主题、正文或...)。我已成功设置菜单,但如果我尝试将菜单项设置为 state:NSOffState,则会出现问题。当我选择一个菜单时,应该关闭之前选择的类别。这是代码:

- (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];
}
于 2014-09-12T04:30:28.300 回答
0

以下应该工作。

// .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];
}
于 2014-09-12T05:22:13.237 回答