2
votes

I have 5 buttons and 5 text fields. I'd like to set up bindings so that button 1 is enabled only if textfields 2-5 have values..... button 2 is enabled only if textfield 1 & 3-5 have values, etc.

I'm just starting to learn about cocoa bindings. I was hoping I could use them to manage the enabling/disabling of the buttons. When I tried to set up an Enable binding on one of the buttons, it won't let me reference the textfields.

Is this possible (and right) to do with bindings?

2

2 Answers

3
votes

You won't be able to reference the textfields directly in the bindings for the bitton.

A way to work around this is to create a NSObject subclass - call it ButtonController - and have instance vars for the 5 textfields:

@inteface ButtonController : NSObject
{
    NSString *text1;
    NSString *text2;
    NSString *text3;
    ...
}

@property(nonatomic, retain) NSString *text1;
@property(nonatomic, retain) NSString *text2;
...

In the xib file, instantiate an object for the ButtonController. Now u can bind things to the button controller instance.

Next, bind all the textfields to the corresponding NSString properties.

Then u can bind the enabled property of the button to ButtonController's properties. When u bind the enabled property of button1, bind it to text2 and check the box for valuetransformer and select "NSIsNotNil". Once u do that, a second enabled2 binding becomes available and u can bind it to text3 etc.

I think this will work but havent tried it yet.

0
votes

Bindings are not conditional, or at least rather opaque in this respect. You will have to do this in code. Check your text fields text change callbacks and enable the buttons accordingly. Should be just a few lines of code.