Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Single selection and bindings



On Fri, 30 Jun 2006 15:19:39 +0100, Jonathan Guy <email@hidden> 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@hidden, <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>



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.