Binding NSPopUpButton to NSArray
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 NSManagedObject
s and they are contained in an NSArray
. I don't want to use an NSArrayController
since I've had lots of trouble changi开发者_如何学编程ng 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?
I think I solved it. I simply created these bindings for NSPopUpButton
:
"Content" to the
items
property (of typeNSArray*
)"Selected Object" to
selectedItem
(of typeItem*
)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.
精彩评论