Skip navigation.
 
mlRe: Keys dependent on NSArrayController's selection
FROM : Sean McBride
DATE : Thu May 08 01:58:42 2008

On 5/7/08 6:16 PM, Dave Dribin said:

>In Interface Builder I bind the button's "Enabled" to 
>"MyController.canEnableButton".  The implementation for 
>canEnableButton is this:
>
>- (BOOL)canEnableButton {
>    NSArray * selectedPeople = [peopleController selectedObjects];
>    if ([selectedPeople count] < 1)
>        return NO;
>
>    Person * selectedPerson = [selectedPeople objectAtIndex:0];
>    return [selectedPerson.lastName hasPrefix: @"D"];
>}
>
>Of course, canEnableButton is now dependent on the selection of 
>NSArrayController.  Thus, I added this:
>
>+ (NSSet *)keyPathsForValuesAffectingCanEnableButton {
>    return [NSSet setWithObjects: 
>@"peopleController.selectionIndexes", nil];
>}
>
>However, this does not trigger the button to update when the selection 
>changes.


Have you put a breakpoint in 'canEnableButton'?  When
'peopleController.selectionIndexes' changes, do you hit the breakpoint?
I suspect not.  The docs for keyPathsForValuesAffectingValueForKey say
keyPaths are ok, but mmalc has mentioned on this list "provided that the
path doesn't include to-many relationships".  This appears to be true in
your case, so probably explains it.  The docs don't mention this
caveat. :(  However, I haven't got keypaths to trigger even with to-one
relationships, which I have been meaning to investigate...

>In observeValueForKeyPath:ofObject:change:context:
>
>    if (context == SelectionIndexesContext)
>    {
>        [self willChangeValueForKey:@"canEnableButton"];
>        [self didChangeValueForKey:@"canEnableButton"];
>    }
>
>This works, however, it just seems wrong to use {will/
>did}ChangeValueForKey: like this.


Also, I seem to remember reading that doing willChange/didChange back-to-
back without actually changing anything is a no-no.  You might check the
archives to confirm my shoddy memory.

--
____________________________________________________________
Sean McBride, B. Eng                <email_removed>
Rogue Research                        www.rogue-research.com
Mac Software Developer              Montréal, Québec, Canada

Related mailsAuthorDate
mlKeys dependent on NSArrayController's selection Dave Dribin May 8, 01:16
mlRe: Keys dependent on NSArrayController's selection Sean McBride May 8, 01:58
mlRe: Keys dependent on NSArrayController's selection Dave Dribin May 8, 08:53
mlRe: Keys dependent on NSArrayController's selection Sean McBride May 8, 16:22
mlRe: Keys dependent on NSArrayController's selection Dave Dribin May 8, 20:45
mlRe: Keys dependent on NSArrayController's selection Sean McBride May 8, 21:02
mlRe: Keys dependent on NSArrayController's selection Hamish Allan Jun 17, 23:26