如何使具有相同选项的多个菜单保持同步?

发布于 2024-10-08 10:02:34 字数 1326 浏览 5 评论 0原文

显然,我的应用程序在菜单栏中有一个主菜单。其中一个菜单项包含一个子菜单,该子菜单也可以在我的应用程序其他位置的 NSPopUpButton 中使用。

到目前为止,我只是在代码中复制菜单选项(以编程方式)来制作弹出按钮,但是当我更改选择器的名称或菜单项的标题等时,我不可避免地会忘记进行相同的更改在弹出按钮中。

我想在一个地方定义菜单并在两个地方使用它。这可能吗?

我尝试了这个:

// Get the same menu used by the main menu
NSMenu *addMenu = [[[[[NSApp mainMenu] itemWithTitle:@"Project"] submenu] itemWithTitle:@"Add"] submenu];

NSMenuItem *item = [[[NSMenuItem alloc] init] autorelease];
[item setImage:[NSImage imageNamed:NSImageNameAddTemplate]];
[item setOnStateImage:nil];
[item setMixedStateImage:nil];

addButton = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(2, 5, 26.0, 16.0)];
[addButton setPullsDown:YES];
[addButton setBordered:NO];
[[addButton cell] setUsesItemFromMenu:NO];
[[addButton cell] setMenuItem:item];
[[addButton cell] setArrowPosition:NSPopUpNoArrow];
[addButton setAutoresizingMask:NSViewMaxXMargin];
[addButton setToolTip:@"Add Files or Directories"];

// Use the same menu as the main menu
[addButton addItemWithTitle:@"Add"];
[[addButton lastItem] setSubmenu:addMenu]; // <--- Breaks here

不幸的是,这会导致运行时错误:

2010-12-19 02:56:11.571 MojiBaker[85384:a0f] * 断言 -[NSMenuItem setSubmenu:] 失败, /SourceCache/AppKit/AppKit-1038.35/Menus.subproj/NSMenuItem.m:695

My application, obviously, has a main menu in the menu bar. One of these menu items contains a submenu that is also available in a NSPopUpButton somewhere else in my app.

Until now I've just been duplicating the menu options (programatically) in code to make the popup button, but when I change the name of the selector, or the title of the menu items etc, I inevitably forget to also make the same changes in the popup button.

I'd like to define the menu in one place and use it in two. Is this possible?

I tried this:

// Get the same menu used by the main menu
NSMenu *addMenu = [[[[[NSApp mainMenu] itemWithTitle:@"Project"] submenu] itemWithTitle:@"Add"] submenu];

NSMenuItem *item = [[[NSMenuItem alloc] init] autorelease];
[item setImage:[NSImage imageNamed:NSImageNameAddTemplate]];
[item setOnStateImage:nil];
[item setMixedStateImage:nil];

addButton = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(2, 5, 26.0, 16.0)];
[addButton setPullsDown:YES];
[addButton setBordered:NO];
[[addButton cell] setUsesItemFromMenu:NO];
[[addButton cell] setMenuItem:item];
[[addButton cell] setArrowPosition:NSPopUpNoArrow];
[addButton setAutoresizingMask:NSViewMaxXMargin];
[addButton setToolTip:@"Add Files or Directories"];

// Use the same menu as the main menu
[addButton addItemWithTitle:@"Add"];
[[addButton lastItem] setSubmenu:addMenu]; // <--- Breaks here

Unfortunately this causes a runtime error with:

2010-12-19 02:56:11.571
MojiBaker[85384:a0f] * Assertion
failure in -[NSMenuItem setSubmenu:],
/SourceCache/AppKit/AppKit-1038.35/Menus.subproj/NSMenuItem.m:695

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

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

发布评论

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

评论(1

迷迭香的记忆 2024-10-15 10:02:34

使用 -copy 完成了这项工作,但后来我注意到所有键盘快捷键都出现在弹出按钮中,这看起来很奇怪,所以我最终只是迭代主菜单的项目并动态构建它,如下所示:

NSMenu *mainAddMenu = [[[[[NSApp mainMenu] itemWithTitle:@"Project"] submenu] itemWithTitle:@"Add"] submenu];
NSMenu *addMenu = [[[NSMenu alloc] initWithTitle:@"Add"] autorelease];
for (NSMenuItem *addMenuItem in [mainAddMenu itemArray]) {
    [addMenu addItemWithTitle:[addMenuItem title] action:[addMenuItem action] keyEquivalent:@""];
}

Using -copy made this work, but then I noticed all the keyboard shortcuts appearing the popup button, which looked weird, so I ended up just iterating the main menu's items and building it dynamically like so:

NSMenu *mainAddMenu = [[[[[NSApp mainMenu] itemWithTitle:@"Project"] submenu] itemWithTitle:@"Add"] submenu];
NSMenu *addMenu = [[[NSMenu alloc] initWithTitle:@"Add"] autorelease];
for (NSMenuItem *addMenuItem in [mainAddMenu itemArray]) {
    [addMenu addItemWithTitle:[addMenuItem title] action:[addMenuItem action] keyEquivalent:@""];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文