Re: NSPopupButton and content values binding transformer
Re: NSPopupButton and content values binding transformer
- Subject: Re: NSPopupButton and content values binding transformer
- From: "email@hidden" <email@hidden>
- Date: Sat, 26 Oct 2013 21:28:59 +0100
On 25 Oct 2013, at 10:58, email@hidden wrote:
> Value transformers applied to NSPopupButton have some idiosyncrasies.
> http://stackoverflow.com/questions/12505764/nspopupbutton-bindings-with-value-transformer
>
> I have applied a transformer to an NSPopupButton content values binding but the IB configured transformer does not seem to be applied.
> I am not alone in this: http://macscripter.net/viewtopic.php?id=39496
>
> If I log the binding the transformer is present.
> However, if I enter an invalid transformer name the app does not raise (which it should).
> If I then log the binding with the invalid name then it does raise.
>
The initial scenario was:
1. NSPopUpButton Content bound to arrayController.arrangedObjects
2. NSPopUpButton Content values bound to arrayController.arrangedObjects.name
3. A value transformer that returns NSString applied to the Content values binding.
This initial scenario does indeed ignore the value transformer.
In order to apply a value transformer to NSPopUpButton Content values the following scenario is required.
1. Don't bind NSPopUpButton Content
2. NSPopUpButton Content values bound to arrayController.arrangedObjects (Note: we do not bind to name).
3. A value transformer that returns NSArray applied to the Content values binding.
The value transformer receives an NSArray and returns a transformed one.
Note that binding content values to arrayController.arrangedObjects.name in the above case will fail.
The value transformer must take responsibility for accessing the name key and transforming it.
It also possible of course to transform the Content binding:
1. NSPopUpButton Content bound to arrayController.arrangedObjects
2. A value transformer that returns NSArray applied to the above Content binding (see below).
However in either of the above cases the Content Objects binding returns the transformed value.
This seems to make it impossible to combine any value transformation with a Content Objects binding.
@implementation MGSTransformer
+ (Class)transformedValueClass {
return [NSArray class];
}
+(BOOL)allowsReverseTransformation {
return NO;
}
-(id)transformedArrayValue:(NSArray*)array
{
NSMutableArray *result = [NSMutableArray array];
for (id value in array)
[result addObject:[self transformedValue:value]];
return result;
}
-(id)transformedValue:(id)value
{
if ([value isKindOfClass:[NSArray class]])
return [self transformedArrayValue:value];
// Do your normal-case transform...
return [[value objectForKey:@"name"] uppercaseString];
}
@end
Jonathan
_______________________________________________
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