Tuesday, January 08, 2008

Cocoa: Preferences - and Binding

Since Mac OS X 10.3 there is a new "Binding" mechanism. The idea behind this is to e.g. have an observer pattern for the enablement of GUI elements or to read and store preferences without coding. To use this, you need to pull the NSUserDefaultsController from the controller palette in InterfaceBuilder into your app. In the inspector for a field, you can then go to the "bindings" pane and bind the values (shown here for a NSTextField, but applies to other GUI elements like checkboxes as well):



Bind to: should be set to the NSUserDefaultsController. For the controller key, you can leave the values. Model key path finally contains the key in the preferences. Depending on the kind of GUI element, the right data type will be choosen automatically.

The technique of binding basically allows to provide a preferences panel without any (Objective-C) coding to set and store the values -- very nice.
It is still a good idea to provide defaults for the settings as described in the previous post.


Update: I just found a nice article from Apple describing all this as well.

No comments:

Post a Comment