如何向 NSPopUpButton(NSMenu) 添加多个具有相同标题的菜单项?

发布于 2024-08-22 10:41:41 字数 284 浏览 12 评论 0原文

正如文档所说,如果两个菜单项具有相同的标题,则不可能将它们添加到 NSPopUpButton 中。我试图将菜单项添加到 [popupButton 菜单],但没有成功。我还尝试创建一个新菜单,向其中添加项目,然后使用 [popupButton setMenu:newMenu],但没有。菜单始终为每个名称仅显示一项。

但我知道这应该是可能的,如果您尝试在 iTunes 中创建智能播放列表,您可以从左侧弹出按钮中选择“播放列表”,从中间选择“=”,右侧将包含每个播放列表的菜单项iTunes 即使它们具有相同的标题。那么他们是如何做到的呢?

As docs say it's impossible to add two menu items to NSPopUpButton if they both have the same title. I was trying to add menu items to [popupButton menu], but with no luck. I was also trying to create a new menu, add items to it and then use [popupButton setMenu:newMenu], but no. Menu always display only one item per name.

But I know it should be possible, if you try to create a smart playlist in iTunes, you could select "Playlist" from the left popup button, "=" from the middle, and the right one will hold menu items for every playlist in iTunes EVEN if they have the same title. So how do they do it?

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

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

发布评论

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

评论(4

泪意 2024-08-29 10:41:41

虽然像 addItemWithTitle: 和 addMenu: 这样的 NSPopUpButton 方法不允许重复名称,但绝对可能有具有相同标题的项目。您只需在 NSMenuItem 本身上设置名称即可。

例如,如果您有一个字符串数组(可能像播放列表名称),您想将它们添加到弹出按钮中,并希望确保其中存在重复项,请按如下操作:

NSArray* items = [NSArray arrayWithObjects:@"Foo", @"Bar", @"Baz", @"Foo", nil];

for (NSString* item in items)
{
   [popupButton addItemWithTitle:@"blah"];
   [[popupButton lastItem] setTitle:item];
   [[popupButton lastItem] setTarget:self];
   [[popupButton lastItem] setAction:@selector(something:)];
}

While NSPopUpButton methods like addItemWithTitle: and addMenu: won't allow duplicate names, it is definitely possible to have items with the same title. You simply have to set the name on the NSMenuItem itself.

For example, if you have an array of strings (like playlist names perhaps) you want to add them to a popup button, and want to make sure duplicates will be in there, do it like this:

NSArray* items = [NSArray arrayWithObjects:@"Foo", @"Bar", @"Baz", @"Foo", nil];

for (NSString* item in items)
{
   [popupButton addItemWithTitle:@"blah"];
   [[popupButton lastItem] setTitle:item];
   [[popupButton lastItem] setTarget:self];
   [[popupButton lastItem] setAction:@selector(something:)];
}
清风无影 2024-08-29 10:41:41

您可以手动创建 NSMenuItem 并将其直接添加到菜单中,而不是使用 addItemWithTitle:。这允许您指定所需的任何标题,并能够将其插入菜单中的任何位置。

NSMenuItem* newItem = [[NSMenuItem alloc] initWithTitle:@"foo" action:@selector(something:) keyEquivalent:@""];

[newItem setTarget:self];
[[popupButton menu] addItem:newItem];
[newItem release];

Instead of using addItemWithTitle:, you can create an NSMenuItem manually and add it directly to the menu. This allows you to specify any title you want, as well as being able to insert it at any location in the menu.

NSMenuItem* newItem = [[NSMenuItem alloc] initWithTitle:@"foo" action:@selector(something:) keyEquivalent:@""];

[newItem setTarget:self];
[[popupButton menu] addItem:newItem];
[newItem release];
涫野音 2024-08-29 10:41:41

我遇到了确切的问题并且很容易解决。我没有使用 NSPopUpButton 方法(例如 –addItemWithTitle:)来操作按钮项目,而是添加了 NSArrayController 并将项目添加到数组控制器中。然后我使用绑定来绑定控制器和弹出按钮,现在它显示具有相同标题的项目。

要进行绑定:

  1. 在 IB 中添加一个 NSArrayController
  2. “Content”NSPopUpButton绑定设置为数组控制器,其中“Controller Key”为“arrangedObjects”
  3. NSPopUpButton"Selected Index" 绑定到数组控制器,其中 "Controller Key""selectionIndex"
  4. [可选] 选择数组控制器并将属性中的类名称设置为您的项目所属的任何类,例如NSString,或者您可以使用默认的NSMutableDictionary和在下面的框中添加键,这样您就可以将项目包装在字典中,并为您想要在弹出按钮中显示的内容以及您想要在后台显示的内容添加不同的键。要设置要在弹出按钮中反映的字典键,请再次转到弹出按钮的“内容”绑定,并将“模态键路径”设置为您在数组控制器属性中添加的键。

I had the exact problem and it was solved easily. Instead of using NSPopUpButton methods such as –addItemWithTitle: to manipulate the button items, I added an NSArrayController and added the items into the array controller instead. Then I used the bindings to bind the controller and the popup button and now it shows items with same titles.

To do the bindings:

  1. Add an NSArrayController in IB.
  2. Set the NSPopUpButton bindings for "Content" to Array Controller with the "Controller Key" being "arrangedObjects"
  3. Set the NSPopUpButton bindings for "Selected Index" to Array Controller with the "Controller Key" being "selectionIndex"
  4. [Optional] Select the array controller and set the Class Name in attributes to whatever class your items are e.g. NSString or you can use the default NSMutableDictionary and add keys in the box below which consequently lets you wrap your items in a dictionary and add different keys for what you want to show in popup button and what you want to have in background. To set which key of the dictionary you want to be reflected in the popup button, go to popup button's bindings for "Content" again and set the "Modal key Path" to the key you added in the array controller attribute.
花开雨落又逢春i 2024-08-29 10:41:41

斯威夫特5
使用保证唯一的标题添加,然后在下一个语句中更改标题。

  • “accountPU”是 NSPopUpButton 对象,
  • “from”是帐户对象。
    accountPU.addItem(withTitle: "\(from.accountID!)")      
    accountPU.lastItem!.title = from.name

Swift 5
Add using guaranteed unique title, then alter title in next statement.

  • "accountPU" is NSPopUpButton object
  • "from" is an account object.
    accountPU.addItem(withTitle: "\(from.accountID!)")      
    accountPU.lastItem!.title = from.name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文