Primarily, Cocoa bindings provides a way to keep an attribute of a view (such as its value or colour) synchronised with a property of a model object (such as a price or an overdue flag). For example, the value displayed in a text field might be kept synchronised with the first name of a Person object. A "binding" is a collection of information that specifies that an attribute of one object should be kept synchronised with what property of another, and modified by what options (to understand the terms "attribute", "property", and key paths, see Key-Value Coding Fundamentals).
You typically establish bindings in Interface Builder. This has the same effect as establishing bindings programmatically using the NSKeyValueBindingCreation method bind:toObject:withKeyPath:options:. This method lets you specify what attribute of the receiver should be kept synchronised with what property of which other object. For example:
[textfield bind:@"value"
toObject:personController
withKeyPath:@"selection.firstName"
options:nil];
The options argument allows the value to be modified in some ways.
What do you have to do?
Typically just set up the bindings in IB. In some situations you may have to establish bindings programmatically. If you're using standard AppKit classes, they implement all the relevant methods already. If you're implementing your own bindings-enabled view class, then there is more work to do(*).
When a binding is established, the bound object (the view or the controller) registers as an observer of the specified property of the object to which it is bound (ultimately the model). The bound object will then be notified of any changes to that property using key-value observing.
What do you have to do?
Typically nothing you aren't already doing (or if you're not, you should be). You should implement and use key-value coding-compliant accessor methods. Support for key-value observing is provided automatically -- invocations of KVC-compliant accessor methods automatically generate notifications. If you don't want to use automatic support, see Automatic Versus Manual Support.
When a value in a view is changed, the new value is passed to the model (after any modification specified by the options) using key-value coding.
[personController setValue:newValue
forKeyPath:@"selection.firstName"];
What do you have to do?
Typically nothing you weren't already doing. You must ensure your model objects are key-value coding compliant.
Information is communicated model >> controller >> view using KVO, but view > controller > model using KVC. Posting KVO notifications from a view is generally not useful (WebView is an anomaly since it combines the roles of controller and view).
For several examples, see Cocoa Bindings Examples and Hints; note in particular the GraphicsBindings example.
[This is the most complicated part.]
If you want to create your own bindings-enabled view, you must implement relevant KVB and KVO registration methods. This is outlined in the bindings documentation, and there are two complete samples (BindingsJoystick and Graphics Bindings). In summary:
In bind:toObject:withKeyPath:options: you record the information relevant to the binding (what property of object what value is being bound to using what options). You also register as an observer of the specified property of the specified object, using addObserver:forKeyPath:options:context:.
You implement observeValueForKeyPath:ofObject:change:context: to receive notifications of changes to observed properties. You must update your corresponding value appropriately.
If your value changes, you must tell the objects to which you're bound using key-value coding.
Courtesy of Allan Odgaard (original version posted to the cocoa-dev mailing list, 28 August 2004).
The main technologies that underly Cocoa bindings are key-value coding (KVC) and key-value observing (KVO).