开发者

why the subclass of NSMenu not responding?

开发者 https://www.devze.com 2023-03-09 22:22 出处:网络
I created a menu resource in a separate xib file, made it to be a subclass NSMenu like the following, and the file\'s owner to be the StatusMenu

I created a menu resource in a separate xib file, made it to be a subclass NSMenu like the following, and the file's owner to be the StatusMenu

@interface StatusMenu : NSMenu
{
@private
    IBOutlet NSMenuItem *menuitem1;
    IBOutlet NSMenuItem *menuitem2;
}

- (IBAction)action1:(id)sender;
- (IBAction)action2:(id)sender;
- (void)show;
@end

where show method is implemented in the following way

- (void)show
{
    NSImage *menuImage = [[NSImage alloc] initWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"myicon" ofType:@"png"]];

    statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain];
    [statusItem setImage:menuImage];
    [statusItem setMenu:self];
    [statusItem setHighlightMode:YES];
}

Then I created and launched the StatusMenu instance in the app delegate like the following

- (void)applicat开发者_Go百科ionDidFinishLaunching:(NSNotification *)aNotification
{
    statusMenu = [[StatusMenu alloc] init];
    statusMenu.user = self.user;
    [statusMenu show];
}

Now, I can see the icon on the status bar. But when I click on the icon, no responding is happening. What could be wrong?


You don't usually subclass NSMenu to do what you're trying to do.

You can simply create another class (call It MenuController or something), put It in your storyboard/xib and have an outlet to It in your AppDelegate.

Here`s a sample project for you to check out.

0

精彩评论

暂无评论...
验证码 换一张
取 消