Search code examples
cocoa-bindings

Bind NSTextField's editable to an NSProgressIndicator state


My form has an activity indicator and a couple text fields. I'd like to use bindings such that if the activity indicator is spinning/visible then the text field's editable flag is disabled. When it stops spinning and disappears then the text field should become editable.

I'm really new to bindings and just can't get this working. If somebody could help provide steps I'd really appreciate it.

I tried to add an NSObjectController and then bind its 'content' to the activity indicator, but then couldn't figure out what to do on the text field bindings.


Solution

  • This is a good question. You'll have to do some sort of indirect binding. You could bind the NSProgressIndicator's Animate binding to a BOOL in your class. Whenever you want to animate it, set the BOOL to YES. However, you'll need to set the NSTextField's binding for either Editable or Enabled (depending on desired effect), and binding it to the same BOOL will result in the opposite affect.

    EDIT: Per TheNextman's comment, you would just set your NSTextField's Value Transformer to NSNegateBooleanTransformerName, and it will take care of enabling/disabling it based on the opposite value of your BOOL.