Re: NSPopUpButton Binding Frustration
Re: NSPopUpButton Binding Frustration
- Subject: Re: NSPopUpButton Binding Frustration
- From: Keary Suska <email@hidden>
- Date: Mon, 30 Jan 2012 09:34:11 -0700
On Jan 29, 2012, at 11:32 PM, Seth Willits wrote:
> This is such a dead simple problem that I think I've encountered a couple times over the years and I don't think I've managed to ever find a solution that works how I want. I'll break this down into an easy specific example.
>
>
> Say I have a file type popup in a save sheet for saving an image:
>
>
> @property NSString * fileType;
>
>
> - (NSArray *)fileTypes
> {
> return [NSArray arrayWithObjects:(id)kUTTypeJPEG, (id)kUTTypePNG, (id)kUTTypeTIFF, nil];
> }
>
>
> - (NSArray *)fileTypeNames
> {
> return [NSArray arrayWithObjects:@"JPEG", @"PNG", @"TIFF", nil];
> }
>
>
>
>
> The popup should display the names, but I want the selected item bound to fileType which is one of the UTIs. By my reading of the documentation, this _should be very simple_.
>
> Bind:
> content -> fileTypes*
> contentValues -> fileTypeNames
> selectedObject -> fileType
>
>
> So content provides the list of objects, contentValues provides the corresponding titles for each object, selectedObject determines which object is selected. Sounds simple, never works. I've never been able to figure it out. It seems that if contents provides an array of *strings*, those strings are use as the titles *no matter what*.
NSPopupButton is a strange animal, and this situation is probably not the only undocumented behavior. For instance, when using selectedObject, the returned object must be an exact object in the content/contentObjects array. -isEqual is not enough.
I suspect that NSPopupButton requires that the contentValue always be a path on the content object. If you change your code to:
- (NSArray *)fileTypes
{
return [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:(id)kUTTypeJPEG, @"uti", @"JPEG", @"name", nil],
[NSDictionary dictionaryWithObjectsAndKeys:(id)kUTTypePNG, @"uti", @"PNG", @"name", nil],
[NSDictionary dictionaryWithObjectsAndKeys:(id)kUTTypeTIFF, @"uti", @"TIFF", @"name", nil],
nil];
}
Bind:
content -> fileTypes
contentObjects -> fileTypes.uti
contentValues -> fileTypes.name
selectedObject -> fileType
You will notice that it works. Note that you may also be able to use an NSDictionaryController to simplify the code.
HTH,
Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden