Re: Using Bindings for a toggle control
Re: Using Bindings for a toggle control
- Subject: Re: Using Bindings for a toggle control
- From: Bill Cheeseman <email@hidden>
- Date: Thu, 26 Apr 2007 08:09:32 -0400
- Thread-topic: Using Bindings for a toggle control
on 2007-04-26 6:39 AM, I. Savant at email@hidden wrote:
> On Apr 26, 2007, at 4:50 AM, Paul Thomas wrote:
>> To make it a little more concrete, what I'm looking at is a Play/
>> Pause button that
>> - displays "Play" and is bound to -play: when -isPlaying is NO
>> - displays "Pause" and is bound to -pause: when -isPlaying is YES
>
> See NSValueTransformer ...
>
> http://developer.apple.com/documentation/Cocoa/Conceptual/
> ValueTransformers/index.html
I do it without a value transformer, like this:
In your window controller class (or whatever), implement a BOOL isPaused
iVar with -isPaused and -setIsPaused: accessor methods for KVC and KVO
compliance.
In IB, give the button a name and an alternate name, in my case, Pause and
Resume. Also in IB, set the button's type to Rounded Bevel Button and its
behavior to Toggle. (Per the HIG and the way buttons of different types are
implemented in Cocoa, a normal push button cannot have alternate state.)
In IB, bind the button's value binding to an NSObjectController object set
to object type NSMutableDictionary whose content is bound to your window
controller class, and set the button's value binding controller key to
selection and key path to isPaused.
Now, your isPaused iVar will be modified to match the button's state every
time you click the button, and if you set the isPaused iVar programmatically
the button state will change to match.
In your action methods or other program code, test the isPaused iVar and
proceed accordingly.
I use a state iVar and bindings instead of a traditional action method,
because in my app the desired action is triggered by a delegate or callback
method in response to events that are outside my control -- my app responds
to incoming events or ignores them depending on whether my app is currently
paused or resumed.
In the OP's case, where you want to perform an action immediately in
response to button clicks, you could achieve the same result much more
simply by abandoning the iVar/accessor/bindings technique altogether.
Instead, implement a single, traditional action method and connect it in IB
to the toggle button. Write the single action method to branch to your play
or pause methods depending on the state of the button itself (that's why
traditional action methods always have a sender parameter: to get the state
of the button or other control). Remember, bindings are not the only way to
write clean code in Cocoa; the traditional techniques still have their
place, and this is one of them. (Assuming, of course, that you don't need
the isPaused iVar value for other purposes.)
--
Bill Cheeseman
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden