为什么 NSMenu 的子类没有响应?
我在单独的 xib 文件中创建了一个菜单资源,使其成为 NSMenu 的子类,如下所示,文件的所有者成为 StatusMenu,
@interface StatusMenu : NSMenu
{
@private
IBOutlet NSMenuItem *menuitem1;
IBOutlet NSMenuItem *menuitem2;
}
- (IBAction)action1:(id)sender;
- (IBAction)action2:(id)sender;
- (void)show;
@end
其中 show 方法按以下方式实现
- (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];
}
然后我在应用程序中创建并启动了 StatusMenu 实例如下所示的委托
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
statusMenu = [[StatusMenu alloc] init];
statusMenu.user = self.user;
[statusMenu show];
}
现在,我可以在状态栏上看到该图标。但是当我点击该图标时,没有任何反应。可能出什么问题了?
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)applicationDidFinishLaunching:(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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您通常不会子类化
NSMenu
来执行您想要执行的操作。您可以简单地创建另一个类(将其命名为
MenuController
或其他名称),将其放入您的storyboard/xib 中,并在您的AppDelegate
中为其提供一个出口。这里有一个示例项目供您查看。
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 yourAppDelegate
.Here`s a sample project for you to check out.