以编程方式创建带有 NSMenuItems 的 NSMenu?

发布于 2024-11-24 18:06:23 字数 723 浏览 3 评论 0原文

首先,我想指出这个问题可能已经被问过,我只是找不到他们的任何答案。

因此,我正在尝试以编程方式为主栏创建 NSMenu 和 NSMenuItem,所以 fe. NSMenu 将是文件,然后它会包含 3x NSMenuItem,新建、打开和保存。

但没有任何效果,这就是我目前所拥有的:

NSMenu *fileMenu = [[NSMenu alloc] initWithTitle:@"File"];
NSMenuItem *newMenu = [[NSMenuItem alloc] initWithTitle:@"New" action:NULL keyEquivalent:@""];
NSMenuItem *openMenu = [[NSMenuItem alloc] initWithTitle:@"Open" action:NULL keyEquivalent:@""];
NSMenuItem *saveMenu = [[NSMenuItem alloc] initWithTitle:@"Save" action:NULL keyEquivalent:@""];
[newMenu setMenu:fileMenu];
[openMenu setMenu:fileMenu];
[saveMenu setMenu:fileMenu];

但是没有任何反应,我很确定我必须告诉应用程序它应该使用 fileMenu,但我该怎么做,如果这不是问题,那么是什么?我对这方面还很陌生,但对学习很感兴趣,所以任何提示都比没有好!

First, I'd like to point out that this question is probably already asked, I just couldn't find any answers from them.

So, I'm programmatically trying to create a NSMenu and NSMenuItem to the main bar, so fe. NSMenu would be File and then it would have 3x NSMenuItem in it, New, Open and Save.

But nothing's working, here's what I have currently:

NSMenu *fileMenu = [[NSMenu alloc] initWithTitle:@"File"];
NSMenuItem *newMenu = [[NSMenuItem alloc] initWithTitle:@"New" action:NULL keyEquivalent:@""];
NSMenuItem *openMenu = [[NSMenuItem alloc] initWithTitle:@"Open" action:NULL keyEquivalent:@""];
NSMenuItem *saveMenu = [[NSMenuItem alloc] initWithTitle:@"Save" action:NULL keyEquivalent:@""];
[newMenu setMenu:fileMenu];
[openMenu setMenu:fileMenu];
[saveMenu setMenu:fileMenu];

But nothing happens, I'm pretty sure I have to tell the application that it should use fileMenu, but how do I do that, and if that's not the problem, then what is? I'm pretty new at this stuff, but interested in learning, so just any tip will be better than nothing!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

以可爱出名 2024-12-01 18:06:23

设置菜单时,您设置的是该项目显示的菜单,而不是其父菜单。

要将这三个项目添加到菜单中,请使用:

[fileMenu addItem: newMenu];
[fileMenu addItem: openMenu];
[fileMenu addItem: saveMenu];

然后将菜单添加到菜单栏:

NSMenuItem *fileMenuItem = [[NSMenuItem alloc] initWithTitle: @"File"];
[fileMenuItem setSubmenu: fileMenu]; // was setMenu:
[[NSApp mainMenu] addItem: fileMenuItem];
[fileMenuItem release];

每个菜单都拥有多个菜单项;一个菜单项可以负责一个子菜单;所有这些菜单都通过 [NSApp mainMenu] 锚定到 UI。

When you set the menu, you set the menu that appears for that item, not its parent menu.

To add those three items to your menu, use:

[fileMenu addItem: newMenu];
[fileMenu addItem: openMenu];
[fileMenu addItem: saveMenu];

And then to add the menu to the menu bar:

NSMenuItem *fileMenuItem = [[NSMenuItem alloc] initWithTitle: @"File"];
[fileMenuItem setSubmenu: fileMenu]; // was setMenu:
[[NSApp mainMenu] addItem: fileMenuItem];
[fileMenuItem release];

Every menu owns multiple menu items; a single menu item can be responsible for a submenu; and all these menus are anchored to the UI by [NSApp mainMenu].

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