2012-06-23 41 views
0

我使用以下绑定排序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到阵列控制器,子菜单先前添加的消失,如图图像下面:

之前添加新项:

enter image description here

enter image description here

添加新的项目后,

我使用的语句添加新NSMenuItem:

[arrayController addObject:[[NSMenuItem alloc]initWithTitle:[newItemTextField stringValue] action:nil keyEquivalent:@""]]; 

,当我们的元素进行排序相同的行为被显示。 任何想法解决这个问题??

+0

你如何添加子菜单:您可以通过包装,增加了该子菜单阵列控制器的代码做到这一点? –

+0

@Jacob这里是我用来添加子菜单的代码'[[popUpButton menu] setSubmenu:defaultSubMenu forItem:[popUpButton itemAtIndex:[indexValue intValue]]]'其中** popUpButton **是_NSPopUpButton_的出口和** indexValue **是_NSTextField_的出口,用户从中输入他/她想要添加子菜单的索引。 – rsharma

回答

0

如果不是将其添加到forItem:[popUpButton itemAtIndex:[indexValue intValue]]而是将它添加到arrayController中的项目中,会发生什么?

我怀疑当你以后在ArrayController中添加一个菜单项时,绑定会导致NSPopUpMenu本身从NSArrayController中的值中创建新的NSMenuItems。而且这些数组控制器中的项目没有子菜单,因为您直接在弹出式按钮中创建了它。

就像您在Array Controller中添加新项目一样,我想您应该将子菜单添加到Array Controller中的项目中。

如果将子菜单添加到数组控制器中的项目中,则看不到子菜单出现在按钮本身中,这可能意味着您需要再次触发绑定,以便弹出按钮将重建其菜单。 [arrayController willChangeValueForKey:@"arrangedObjects"];[arrayController didChangeValueForKey:@"arrangedObjects"];

+0

'[arrayController willChangeValueForKey:@“arrangedObjects”]; [[[arrayController arrangedObjects] objectAtIndex:[indexValue intValue]] setSubmenu:defaultSubMenu]; [arrayController didChangeValueForKey:@“arrangedObjects”];'这是你的意思是包装,或者我只是误解@Jacob – rsharma

+0

是的,这就是我的意思。 –

+0

它不工作。但是,正在添加子菜单(_I使用NSLog打印arrayController,我可以看到在console_上添加了一个子菜单)。但子菜单尚未显示在弹出式按钮中。有任何想法吗? – rsharma