0

ユーザーが左側の矢印をクリックしたときにメニューとして表示されるいくつかのカテゴリを表示する 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 に設定する必要があります。それ、どうやったら出来るの?

ご協力ありがとうございました。

ここに画像の説明を入力

4

2 に答える 2

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];
}
于 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 に答える