我使用以下绑定排序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