Re: Single selection and bindings
Re: Single selection and bindings
- Subject: Re: Single selection and bindings
- From: Matt Neuburg <email@hidden>
- Date: Fri, 30 Jun 2006 09:51:05 -0700
- Thread-topic: 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).
It is probably easy to do, but I don't think you're necessarily going to do
it with zero code. I think there is a slight tendency to confuse the notion
"Oh wow, I'm going to use bindings" with "Oh wow, I'm not going to write any
code", whereas in reality nothing could be further from the truth - a little
code goes very well with making bindings work.
So, for example, although this doesn't answer your question directly, let's
pretend you asked about an NSArrayController instead of a tree controller
(because I've never used a tree controller so I don't know about that one).
Well then, what you're asking about is obviously the value of
selection.@count.
So you could bind the Enabled of the button to the controller's selection
(controller key) and @count (model key path).
But this isn't quite enough, because now the button is disabled when there
is no selection (@count is 0, meaning false) and when there is a single or
multiple selection (@count is 1 or more, meaning true). So we might
introduce a transformer here to solve the problem:
- (id)transformedValue:(id)value {
return (([value intValue] > 1) ? [NSNumber numberWithBool:NO] : value);
}
So there's a solution, with just one line of relevant code (and about four
more lines of boilerplate, not shown).
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:
This email sent to email@hidden