Search code examples
cocoafocusnstableviewnsviewnsmenuitem

NSTableView and NSSearchField in an NSView, which is the outlet of an NSMenuItem, don't work


I'm having an NSView, which is set as the view: outlet of an NSMenuItem. The view contains an NSTableView (inside an NSScrollView) and an NSSearchField. The NSMenu is shown when the user clicks a specific NSStatusItem. When I launch the application from Xcode (Build and Run), the controls behave well, but whenever I launch it from the Finder (like the users would), they don't work and don't take any focus. My application is an LSUIElement. Can anyone help me out? Thanks.


Solution

  • Since, your application is an UIElement, it's NSMenu won't take focus. You must set an NSTimer to 0.01 seconds to show the NSMenu, and you should make the application active:

    - (void)statusItemClicked {
        [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(showMenu) userInfo:nil repeats:NO];
        [NSApp activateIgnoringOtherApps:YES];
    }
    - (void)showMenu {
        [statusItem popUpStatusItemMenu:statusMenu];
    }