Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSMenuItem action is not executing

I am creating a status bar on my app and the menus show up fine, however it does not seem to access the actions that I have set up for it.

I am adding the status bar through a C function:

void MyFunction(CefRefPtr<CefBrowser> browser) {
    MyMenuItem* item = [[MyMenuItem alloc] initWithTitle:@"Item 1" action:@selector(doAction:) keyEquivalent:@"")];
    [item setBrowser:browser];

    NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
    [menu setAutoenablesItems:NO];
    [menu addItem:item];

    NSStatusBar *statusBar = [NSStatusBar systemStatusBar];
    MSStatusItem *statusItem = [statusBar statusItemWithLength:NSBariableStatusItemLength];
    [statusItem retain];
    [statusItem setImage:imageObj];
    [statusItem setTitle:@"Status"];
    [statusItem setHilightMode:YES];
    [statusITem setMenu:menu];      
}

Declaring the actions right above MyFunction():

@interfaceMyMenuItem : NSMenuItem {
    CefRefPtr<CefBrowser>_browser;
}

@property (nonatomic) CefRefPtr<CefBrowser> browser;
- (void)doAction:(id)sender;
@end

@implementation MyMenuItem
@synthesize browser = _browser;
- (void)doAction:(id)sender {
    NSLog(@"Doing action");
}
@end

Everything compiles just fine, So I think it might have something to do with creating the Items from a C function, but not properly setting up the interface.

like image 788
user3546429 Avatar asked Jan 11 '23 18:01

user3546429


1 Answers

have you tried setting target of menuitem and implementing validateUserInterfaceItem?

MyMenuItem* item = [[MyMenuItem alloc] initWithTitle:@"Item 1" action:@selector(doAction:) keyEquivalent:@"")];
[item setTarget:self];

and

-(BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)anItem
{
    return ([anItem selector] == @selector(doAction:)); 
}
like image 122
lead_the_zeppelin Avatar answered Jan 20 '23 18:01

lead_the_zeppelin