在 Cocoa 中为 NSMenuItem 设置目标/操作的正确方法?

发布于 2024-10-20 14:51:06 字数 2180 浏览 5 评论 0原文

我在执行一些最初的 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
  • 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撧情箌佬 2024-10-27 14:51:06

以编程方式,您尝试过吗:

[statusItem setTarget:someTarget];
[statusItem setAction:@selector(someSelector)];

它应该可以工作。

Programmatically, have you tried :

[statusItem setTarget:someTarget];
[statusItem setAction:@selector(someSelector)];

It should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文