【发布时间】:2012-06-23 09:19:27
【问题描述】:
我正在使用以下绑定对 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 的 NSMutable 数组
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];
并且排序工作正常。
现在的问题是,如果我将子菜单添加到任何菜单项,然后我将新的 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 的一个出口,用户可以从中输入他/她想要添加子菜单的索引。
标签: objective-c cocoa