On 3 May 2013, at 17:07, Andy Lee <ag...@mac.com> wrote:

> On May 3, 2013, at 1:29 AM, Gerriet M. Denkmann <gerr...@mdenkmann.de> wrote:
>> I have a button which only makes sense when exactly one thing is selected.
>> Currently it's Enabled property is bound to Array 
>> Controller.selectedObjects.@count.
>> I.e. the button is enabled if one or more things are selected.
>> 
>> But I want it to be disabled when nothing is selected AND also if more than 
>> one thing is selected.
>> Is there a way to do this without code?
>> 
>> If not - what to do ? Create a value transformer? Or what else?
> 
> I vaguely remember doing this by adding a readonly property to the array 
> controller -- something like "exactlyOneIsSelected" -- with a corresponding 
> getter method. Then you can bind to ArrayController.exactlyOneIsSelected.
> 
> Then the question would be how to trigger KVO on that property. I don't 
> remember what I did. My first thought would be to implement +(NSSet 
> *)keyPathsForValuesAffectingExactlyOneIsSelected, but I'm not sure what to 
> return. Maybe @"selectionIndexes"? Would @"selectedObjects.@count" work? I 
> don't know if you can use aggregation in keyPaths...Affecting... methods.
> 
> It might be useful to implement these methods on a category of 
> NSArrayController if you need this logic in multiple places.
> 
> As usual I have a nagging feeling there's a better way, but this at least 
> should work.

An interesting idea.

I created a ValueTransformer like:

@implementation UniqueTransformer

+ (Class)transformedValueClass { return [ NSNumber class ]; }

- (id)transformedValue:(id)value
{
        BOOL unique = [ value respondsToSelector: @selector(integerValue) ]  && 
 [ value integerValue ] == 1;
        return @(unique);
}

@end

Seems to me rather less code. Maybe still not the best way to do it, but the 
best I could think of.


Kind regards,

Gerriet.



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to