将NSPopUpButton绑定到NSArray

时间:2011-09-29 20:26:18

标签: nsarray nspopupbutton

我对NSPopUpButton的绑定感到有些失落。我有一个自定义类,它包含一系列我想在弹出窗口中显示的项目。这些项是NSManagedObject的子类,它们包含在NSArray中。我不想使用NSArrayController,因为我在以编程方式更改选择时遇到了很多麻烦,感觉就像实现一样混乱。

问题很简单,我不知道如何正确地将数组绑定到弹出窗口。我所要做的就是在弹出菜单中列出数组项,但标题是核心数据URI。我相信我可以使用description方法来更改标题,但这听起来不太合理。

如何正确地将NSArray绑定到NSPopUpButton

1 个答案:

答案 0 :(得分:0)

我想我解决了。我只是为NSPopUpButton

创建了这些绑定
  1. items属性的“内容”(类型为NSArray*

  2. “选定对象”到selectedItem(类型为Item*

  3. 最后将“内容值”添加到items.name

  4. 对于第三个绑定,我实现了valueForKeyPath:

    - (id)valueForKeyPath:(NSString *)keyPath
    {
        NSArray *components = [keyPath componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"."]];
        if ([components count] == 2 && [components objectAtIndex:0] == @"items")
        {
            return [self.items valueForKey:[components objectAtIndex:1]];
        }
        return [super valueForKeyPath:keyPath];
    }
    

    第三个绑定可能也是标题的单独数组,但我更灵活。