将 NSPopUpButton 绑定到 NSArray

发布于 12-07 04:13 字数 368 浏览 5 评论 0原文

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

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

有什么想法如何正确地将 NSArray 绑定到 NSPopUpButton 吗?

I'm a little lost with bindings on NSPopUpButton. I have a custom class that holds an array of items that I'd like to display in a pop up. These items are subclasses of NSManagedObjects and they are contained in an NSArray. I don't want to use an NSArrayController since I've had lots of trouble changing the selection programmatically and it feels like cluttering the implementation.

The problem is simply that I don't know how to bind the array properly to the pop up. All I have managed to do is list the array items on the pop up menu, but the titles are core data URIs. I believe I could use the description method to change the title, but this does not sound very advisable.

Any ideas how to bind NSArray to the NSPopUpButton properly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慢慢从新开始2024-12-14 04:13:02

我想我解决了。我只是为 NSPopUpButton 创建了这些绑定:

  1. "Content" 到 items 属性(类型为 NSArray*

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

  3. 最后“Content Values”到items.name

对于第三个绑定,我实现了 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];
}

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

I think I solved it. I simply created these bindings for NSPopUpButton:

  1. "Content" to the items property (of type NSArray*)

  2. "Selected Object" to selectedItem (of type Item*)

  3. Finally "Content Values" to items.name

For the third binding I implemented 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];
}

The third binding could've been also a separate array for titles, but I this is much more flexible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文