NSSearchField类别菜单状态 [英] NSSearchField Category Menu State

查看:37
本文介绍了NSSearchField类别菜单状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSSearchField控件,我想在其中显示一些类别,当用户单击左侧的箭头时,这些类别将显示为菜单.阅读苹果的文档后,我有了一些想法.以下是我的代码.

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 {

    }
}

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

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?

感谢您的帮助.

推荐答案

以下内容应该有效.

// .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];
}

这篇关于NSSearchField类别菜单状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆