objective-ccocoansmenunsstatusbar

NSMenu delegate does not trigger menuWillOpen


I've got an Interface Builder NSMenu-Object. I added my custom Class "StatusBarMenu".

The StatusBarMenu.h file looks like this:

@interface StatusBarMenu : NSMenu <NSMenuDelegate>

The StatusBarMenu.c file looks like this:

- (void)menuWillOpen:(NSMenu *)menu {
    `NSLog(@"open");`
}

The Menu is assigned in the AppDelegate.c as follows:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    self.statusBar = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
    self.statusBar.highlightMode = YES;
    [self.statusBar setMenu:self.statusMenu];
}

And the AppDelegate.h:

#import <Cocoa/Cocoa.h>
#import "StatusBarMenu.h"
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (strong, nonatomic) NSStatusItem *statusBar;
@property (weak) IBOutlet StatusBarMenu *statusMenu;

The Menu opens, but menuWillOpen is not called. Did i miss anything?

I appreciate any help! Thanks.


Solution

  • You have to set the delegate, either in Interface Builder by a connection (statusMenu > delegate to AppDelegate) or in code in applicationDidFinishLaunching

    self.statusMenu.delegate = self;