Skip navigation.
 
mlRe: Single selection and bindings
FROM : Matt Neuburg
DATE : Fri Jun 30 19:13:58 2006

On Fri, 30 Jun 2006 15:19:39 +0100, Jonathan Guy <<email_removed>> said:

> This would seem an easy thing to do but can't figure it out. I want to bind
> the "Enabled" part of a button to the selection of a tree controller BUT I
> only want the button to be enable when there is a single item selected in the
> outline view (NOT a zero or multiple selection).


Another solution (besides the value transformer approach from my earlier
message) would be a controller subclass. In the case of NSArrayController,
we could bind the button's Enabled to an ivar (because that's an
observable). The ivar is set by code that takes advantage of the fact that
selectedObjects, too, is an observable:

@implementation MyNSArrayController

- (void) awakeFromNib {
  [self addObserver:self forKeyPath:@"selectedObjects"
            options:0 context:nil];
}

- (void)observeValueForKeyPath:(NSString *)keyPath
            ofObject:(id)object change:(NSDictionary *)change
            context:(void *)context {
  [self setValue:
      [NSNumber numberWithBool:
        [[self selectedObjects] count] == 1]
      forKey: @"selectionIsOne"];
}

@end

So we bind the button's Enabled to the controller's "selectionIsOne" and
presto, we're there.

m.

--
matt neuburg, phd = <email_removed>, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide - Second Edition!
<http://www.amazon.com/gp/product/0596102119>

Related mailsAuthorDate
mlSingle selection and bindings Jonathan Guy Jun 30, 16:19
mlRe: Single selection and bindings George Orthwein Jun 30, 17:27
mlRe: Single selection and bindings Scott Anguish Jun 30, 18:00
mlRe: Single selection and bindings Matt Neuburg Jun 30, 18:51
mlRe: Single selection and bindings Matt Neuburg Jun 30, 19:13
mlRe: Single selection and bindings Jonathan Guy Jul 5, 14:43