在 Cocoa 中为 NSMenuItem 设置目标/操作的正确方法?
我在执行一些最初的 Cocoa 编程时遇到了一些真正的困难。
本质上,我有一个 NSStatusBar
项目,其中附加了一个 NSMenu
作为菜单。该菜单有一个NMMenuItem
。在 IB 中,我已将 NSMenuItem 连接到 NSObject ,该 NSObject 本身设置为 ApplicationDelegate 的类;然后,我将接收到的操作设置为 ApplicationDelegate 中的 IBAction 方法。我认为一切都正确连接,除了当我运行程序并单击菜单项时,不调用 IBAction 方法。我实在是看不下去了。这是相关代码。
应用程序委托 h 文件:
#import <Cocoa/Cocoa.h>
@interface sssAppDelegate : NSObject <NSApplicationDelegate> {
IBOutlet NSMenu *statusMenu;
NSStatusItem *statusItem;
}
- (IBAction)showPreferencePanel:(id)sender;
@end
应用程序委托 m 文件:
#import "sssAppDelegate.h"
@implementation sssAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
}
-(void)awakeFromNib{
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain];
[statusItem setMenu:statusMenu];
[statusItem setTitle:@"Status"];
[statusItem setHighlightMode:YES];
}
- (IBAction)showPreferencePanel:(id)sender {
NSLog(@"Hello World!");
}
@end
正如我所说,在 IB 中,我已将 NSMenu
连接到应用程序委托中的 statusMenu
(因此菜单全部显示在NSStatusBar
),并且我已使用 Application Delegate 类将 NSMenu
中的 NSMenuItem
连接到 NSObject
,并且将其连接到调用 showPreferencePanel,但是当我运行它时没有任何反应!
我也以编程方式尝试过,但仍然无法调用 IBAction 方法。
编辑:我会附上一些屏幕截图来显示 IB 中的设置,但我还没有被允许。
主笔尖包含添加到 NSStatusBar
的菜单,如下所示:
- FO
NSApplication
- FR
FirstResponder
- Application
NSApplication< /code>
- 字体管理器
NSFontManager
- 主菜单
NSMenu
- 菜单项(首选项)
NSMenuItem
- 菜单项(首选项)
- Sss 应用程序委托
sssAppDelegate
NSMenuItem:
- 发送操作 -
showPreferencePanel
--->Sss应用程序委托
Sss 应用程序委托:
- 插座 -
statusMenu
--->主菜单
- 收到的操作 -
showPreferencePanel:
---> ;主要项目(首选项)
I'm having some real difficulty with some initial Cocoa programming I am carrying out.
Essentially, I have an NSStatusBar
item with an NSMenu
attached as the menu. The menu has a single NMMenuItem
. In IB I have connected the NSMenuItem to an NSObject
which itself is set to the ApplicationDelegate's class; I have then set the Received Actions to an IBAction
method in the ApplicationDelegate. Everything is hooked up correctly I think, except when I run the program and click the menu item the IBAction
method is not called. I really can't seem to work it out. Here is the relevant code.
Application Delegate h file:
#import <Cocoa/Cocoa.h>
@interface sssAppDelegate : NSObject <NSApplicationDelegate> {
IBOutlet NSMenu *statusMenu;
NSStatusItem *statusItem;
}
- (IBAction)showPreferencePanel:(id)sender;
@end
Application Delegate m file:
#import "sssAppDelegate.h"
@implementation sssAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
}
-(void)awakeFromNib{
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain];
[statusItem setMenu:statusMenu];
[statusItem setTitle:@"Status"];
[statusItem setHighlightMode:YES];
}
- (IBAction)showPreferencePanel:(id)sender {
NSLog(@"Hello World!");
}
@end
As I say, in IB I have connected the NSMenu
to statusMenu
in the Application Delegate (thus the menu all shows up under the NSStatusBar
), and I have connected an NSMenuItem
within the NSMenu
to the NSObject
with the Application Delegate class, and hooked it up to call showPreferencePanel, but nothing happens when I run it!!!
I tried it programatically as well but still can't get the IBAction method to be called.
Edit: I would attach some screen grabs to show the setup in IB but I'm not yet allowed.
The main nib which contains the menu that is added to the NSStatusBar
, it looks like this:
- FO
NSApplication
- FR
FirstResponder
- Application
NSApplication
- Font Manager
NSFontManager
- Main Menu
NSMenu
- Menu Item (Preferences)
NSMenuItem
- Menu Item (Preferences)
- Sss App Delegate
sssAppDelegate
NSMenuItem:
- Sent Actions -
showPreferencePanel
--->Sss App Delegate
Sss App Delegate:
- Outlets -
statusMenu
--->Main Menu
- Received Actions -
showPreferencePanel:
--->Main Item (Preferences)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以编程方式,您尝试过吗:
它应该可以工作。
Programmatically, have you tried :
It should work.