如何使具有相同选项的多个菜单保持同步?
显然,我的应用程序在菜单栏中有一个主菜单。其中一个菜单项包含一个子菜单,该子菜单也可以在我的应用程序其他位置的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
-copy
完成了这项工作,但后来我注意到所有键盘快捷键都出现在弹出按钮中,这看起来很奇怪,所以我最终只是迭代主菜单的项目并动态构建它,如下所示: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: