7

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?

4

4 に答える 4

5

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:)];
}
于 2010-02-24T06:39:41.927 に答える
3

を使用する代わりにaddItemWithTitle:、NSMenuItem を手動で作成し、メニューに直接追加できます。これにより、必要なタイトルを指定したり、メニューの任意の場所に挿入したりできます。

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

[newItem setTarget:self];
[[popupButton menu] addItem:newItem];
[newItem release];
于 2010-02-25T00:00:24.400 に答える
1

正確な問題があり、簡単に解決できました。–addItemWithTitle:などのNSPopUpButtonメソッドを使用してボタン項目を操作する代わりに、NSArrayControllerを追加し、代わりに配列コントローラーに項目を追加しました。次に、バインディングを使用してコントローラーとポップアップ ボタンをバインドすると、同じタイトルのアイテムが表示されるようになりました。

バインディングを行うには:

  1. IB にNSArrayControllerを追加します。
  2. 「コンテンツ」NSPopUpButtonバインディングを配列コントローラーに設定し、「コントローラー キー」を「arrangedObjects」に設定します。
  3. 「Selected Index」NSPopUpButtonバインディングを、 「Controller Key」「selectionIndex」の配列コントローラーに設定します。
  4. [オプション] アレイ コントローラを選択し、属性のクラス名を項目のクラスに設定します。ポップアップボタンに表示したいものと背景に表示したいもの。ポップアップボタンに反映させたい辞書のキーを設定するには、再度ポップアップボタンの「コンテンツ」のバインディングに移動し、 「モーダルキーパス」を配列コントローラー属性に追加したキーに設定します。
于 2010-03-01T21:36:05.717 に答える