Search code examples
cocoabindingkey-value-observingnstextfield

Cocoa: Avoiding 'Updates Continuously' in control binds


I have several panels that contain NSTextField controls bound to properties within the File's Owner object. If the user edits a field and then presses Tab, to move to the next field, it works as expected. However if the user doesn't press Tab and just presses the OK button, the new value is not set in the File's Owner object.

In order to workaround this I have set Updates Continuously in the binding, but this must be expensive (EDIT: or at least it's inelegant).

Is there a way to force the bind update when the OK button is pressed rather than using Updates Continuously?


Solution

  • You're right that you don't need to use the continuously updates value option.

    If you're using bindings (which you are), then what you should be doing is calling the -commitEditing method of the NSController subclass that's managing the binding. You'd normally do this in your method that closes the sheet that you're displaying.

    -commitEditing tells the controller to finish editing in the active control and commit the current edits to the bound object.

    It's a good idea to call this whenever you are performing a persistence operation such as a save.