Binding different elements of an UI.
-
I'm wondering if there's a way to bind the different elements of a UI
together. (For example, have a NSTextField that is visible only when a
certain checkbox is checked or radio button selected.) Would I have to
make an outlet for a controller or the document and connect the checkbox
to the outlet and a "toggle" action or something?
(If this is too basic of a question for this forum, is there another forum
somewhere where I can post my newbie questions?) -
> I'm wondering if there's a way to bind the different elements of a
> UI together. (For example, have a NSTextField that is visible only
> when a certain checkbox is checked or radio button selected.)
> Would I have to make an outlet for a controller or the document and
> connect the checkbox to the outlet and a "toggle" action or something?
Create an instance of NSObjectController in the nib file, and bind
your values to that. This is called 'Cocoa Bindings'. Look at the
"Bindings" section of the inspector pane for the UI elements in this
example .nib file.
--sjd; -
On Sat, 02 Sep 2006 10:52:36 -0600, "Keith Penrod" <keith.penrod...>
said:
> I'm wondering if there's a way to bind the different elements of a UI
> together. (For example, have a NSTextField that is visible only when a
> certain checkbox is checked or radio button selected.)
Sure, just bind both the checkbox's "value" and the text field's "enabled"
to the same thing. That's all there is to it. You don't even need code to
test this - in fact, you don't even need an Xcode project. Start up
Interface Builder and ask for a new Application project. Drag a checkbox
(switch) and an NSTextField into the design window, and drag an
NSObjectController into the main nib window. Now:
* Select the NSObjectController, command-1, check Automatically Prepares
Content
* Select the checkbox, command-4, bind its "value" to the
NSObjectController's selection.thing
* Select the NSTextField, command-4, bind its "enabled" to the
NSObjectController's selection.thing
Now command-R and try it out. Sure enough, the NSTextField enables and
disables as you check and uncheck the checkbox
m.
--
matt neuburg, phd = <matt...>, <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> -
On Sep 2, 2006, at 9:52 AM, Keith Penrod wrote:
> I'm wondering if there's a way to bind the different elements of a
> UI together. (For example, have a NSTextField that is visible only
> when a certain checkbox is checked or radio button selected.)
> Would I have to make an outlet for a controller or the document and
> connect the checkbox to the outlet and a "toggle" action or something?
The best thing to do is change the way you're thinking about the
problem. There's a clue in the language above: "An NSTextField that
is visible only when a certain checkbox is checked or radio button
selected."
Instead, think of it as binding the text field's visibility to a
condition. Now what do you use to change the condition? A checkbox
or radio button selection. So those are *also* bound to the same
attribute, or some attribute that feeds into that condition.
Breaking the problem apart in this fashion -- hooking views to a data
model, rather than hooking views to each other -- will help lead you
to good MVC application design more often than not. And everything
in Cocoa is built on an MVC architecture, so you'll be going with the
grain of the framework if you do so.
-- Chris



