NSSearchField类别菜单状态

时间:2014-09-12 04:25:59

标签: objective-c macos cocoa nsmenuitem nssearchfield

我有一个NSSearchField控件,我想在用户点击左侧的箭头时显示一些显示为菜单的类别。在阅读Apple的文档后,我已经有了一些想法。以下是我的代码。

// .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 {

    }
}

下面的屏幕截图显示了结果。现在,我需要将选择状态(无论选择哪个)设置为1,以便显示复选标记。我该怎么做?

感谢您的帮助。

enter image description here

2 个答案:

答案 0 :(得分:1)

我想添加一个类别菜单(例如:搜索主题,正文或...)  在我的NSSearchField中。我已成功设置菜单,但有一个  如果我尝试将menuitem设置为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];
}

答案 1 :(得分: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];
}