选择NSMenuItem时,NSPopUpButton不会更新为图像

时间:2014-09-19 08:46:51

标签: macos nspopupbutton nspopupbuttoncell

我正在使用带有pullDown的NSPopUpButton,并在菜单中添加了一些菜单项(图像和标题),并将该菜单添加到Button单元格中。

最初我将菜单的第一个菜单项指定给要在NSPopUpButton中显示的Button单元格。

当我点击NSPopUpButton时,会显示一个菜单,其中包含我之前添加的所有项目,当我从菜单中选择一个项目时,其数据不会更新为NSPopUpButton。即标题正在更新,图像不是..

任何人都可以建议我如何解决这个问题

我写的代码:

In ButtonCell class:

NSMenu* newMenu = [[NSMenu alloc] initWithTitle:@""];
    [newMenu setAutoenablesItems:YES];
    [[self optionMenuController] insertItemsInMenu:newMenu atIndex:0];
    if(isFromSetOption)
    {
        [self setMenuItem:[newMenu itemAtIndex:1]];
        isFromSetOption = NO;
    }
    //[newMenu insertItemWithTitle:@"dummy" action:nil keyEquivalent:@"" atIndex:0];
    [self setMenu:newMenu];

    [newMenu release];

In OptionMenuController :
- (void) insertItemsInMenu:(NSMenu*)menu atIndex:(NSInteger)insertIndex
{
    for( NSMenuItem* item in [self createMenuItems] )
    {
        [menu insertItem:item atIndex:insertIndex++];
    }
}
- (NSMenuItem*) menuItemForOption:(AMOptionMenuItem*)option inGroup:(AMOptionMenuItem*)group
{
    NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:[option title] action:@selector(optionChosen:) keyEquivalent:@""];

    [menuItem setIndentationLevel:1];
    [menuItem setTarget:self];
    [menuItem setEnabled:YES];
    [menuItem setImage:[NSImage imageNamed:[option imageTitle]]];
  /*  if([[option identifier] isEqualToString:@"Color"])
    {
        [menuItem setImage:[NSImage imageNamed:@"cabinet"]];
    }
    else{
        [menuItem setImage:[NSImage imageNamed:@"YieldIcon.png"]];
    }
    */


    NSString* keypath = [NSString stringWithFormat:@"%@.is%@", [group identifier], [option identifier]];
    [menuItem setRepresentedObject:keypath];

    NSDictionary *bindingOptions = nil;
    [menuItem bind:@"value" toObject:self withKeyPath:keypath options:bindingOptions];

    return [menuItem autorelease];
}

0 个答案:

没有答案
相关问题