子菜单项不可点击

时间:2014-10-16 12:59:27

标签: objective-c cocoa

我想要一个动态子菜单。现在我尝试了这种方式:

@interface AppController()
@property(strong) Accounts * accView;
@property (weak) IBOutlet NSMenuItem *serverMenu;
@end

@implementation AppController

-(void)awakeFromNib {
    _accView = [[Accounts alloc] initWithNibName:@"Accounts" bundle:nil];
    [self.viewk addSubview:[_accView view]];
    [[_accView view] setFrame:self.viewk.bounds];

    NSMenuItem * testItem = [[NSMenuItem alloc] initWithTitle:@"Test" action:@selector(test:) keyEquivalent:@""];

    [[_serverMenu submenu] addItem:testItem];

}

- (void)test:(id)test {

}

然后它被添加但我无法点击它。就像这样:

enter image description here

如何启用它?

1 个答案:

答案 0 :(得分:1)

您还需要为NSMenuItem分配目标,例如

[testItem setTarget:self];