我使用以下綁定排序NSPopUpButton:維護子菜單中NSPopUpButton
[arrayController bind:@"contentArray" toObject:self withKeyPath:@"displayElements" options:nil];
[popUpButton bind:@"content" toObject:arrayController withKeyPath:@"arrangedObjects" options:nil];
[popUpButton bind:@"contentValues" toObject:arrayController withKeyPath:@"arrangedObjects.title" options:nil];
凡displayElements是NSMenuItem
displayElements = [[NSMutableArray alloc] initWithObjects:[[NSMenuItem alloc]initWithTitle:@"one" action:nil keyEquivalent:@""],
[[NSMenuItem alloc]initWithTitle:@"two" action:nil keyEquivalent:@""],
[[NSMenuItem alloc]initWithTitle:@"three" action:nil keyEquivalent:@""],
nil];
的NSMutable陣列和排序工作得很好。
現在的問題是,如果我的子菜單添加到任何菜單項目之後,我添加一個新的NSMenuItem到陣列控制器,子菜單先前添加的消失,如圖圖像下面:
之前添加新項:
:添加新的項目後,
我使用的語句添加新NSMenuItem:
[arrayController addObject:[[NSMenuItem alloc]initWithTitle:[newItemTextField stringValue] action:nil keyEquivalent:@""]];
,當我們的元素進行排序相同的行爲被顯示。 任何想法解決這個問題??
你如何添加子菜單:您可以通過包裝,增加了該子菜單陣列控制器的代碼做到這一點? –
@Jacob這裏是我用來添加子菜單的代碼'[[popUpButton menu] setSubmenu:defaultSubMenu forItem:[popUpButton itemAtIndex:[indexValue intValue]]]'其中** popUpButton **是_NSPopUpButton_的出口和** indexValue **是_NSTextField_的出口,用戶從中輸入他/她想要添加子菜單的索引。 – rsharma