我有一个配置了绑定和核心数据的 NSPopUpButton。一切正常,但是我想添加一个实现“编辑列表”操作的项目,比如
Item 1
Item 2
Item 3
Item 4
------
Edit List..
这可能与绑定有关吗?
我认为答案是否定的,至少不完全是。我以为我会以编程方式向按钮提供内容并维护Selected Value的绑定,所以这就是我想出的
- (void)updateSectorPopupItems
{
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Sector"];
NSSortDescriptor *sortPosition = [[NSSortDescriptor alloc] initWithKey:@"position" ascending:YES];
[request setSortDescriptors:@[sortPosition]];
NSError *anyError = nil;
NSArray *fetchObjects = [_gdcManagedObjectContext executeFetchRequest:request
error:&anyError];
if (fetchObjects == nil) {
DLog(@"Error:%@", [anyError localizedDescription]);
}
NSMutableArray *sectorNames = [NSMutableArray array];
for (NSManagedObject *sector in fetchObjects) {
[sectorNames addObject:[sector valueForKey:@"sectorCatagory"]];
}
[_sectorPopUpBotton addItemsWithTitles:sectorNames];
NSInteger items = [[_sectorPopUpBotton menu] numberOfItems];
if (![[_sectorPopUpBotton menu] itemWithTag:1] ) {
NSMenuItem *editList = [[NSMenuItem alloc] initWithTitle:@"Edit List..." action:@selector(showSectorWindow:) keyEquivalent:@""];
[editList setTarget:self];
[editList setTag:1];
[[_sectorPopUpBotton menu] insertItem:editList atIndex:items];
}
我遇到的几个问题
1)当使用添加菜单项时
[_sectorPopUpBotton menu] insertItem:editList atIndex:items];
无论在 atIndex 中输入什么值,该项目始终出现在菜单列表的顶部。
2)我只想让“编辑列表...”菜单项启动操作,如何防止它被选为值?