Bindings (sometimes referred to as Cocoa bindings) are a set of technologies that enables you to easily establish a connection between an item of data and its presentation in a view. Any change made to the value of the data, in either the view or the object that stores the data, is automatically propagated across the connection. Cocoa bindings reduce the glue code that developers once had to write to enable communication between the view and model layers of your applications. In addition, the Interface Builder application lets you quickly establish bindings between the objects of your application.
Cocoa bindings is based on the Model-View-Controller, object modeling, and Mediator design patterns. In concrete terms, a binding “binds” an attribute of a view object, through a mediating controller object, to a property of a model object so that a change in either attribute or property is automatically reflected in the other.
Several Technologies Enable Cocoa Bindings
The Cocoa bindings feature relies on several technologies that work together to specify how objects synchronize their values across MVC boundaries:
Key-value binding (KVB). This protocol enables a class to establish and remove bindings between objects and to advertise the bindings that it exposes. An object must watch for relevant changes in the object to which it is bound and react to those changes.
Key-value coding (KVC). This protocol specifies how you can access a property in an object using the property’s name as the key to the property’s value. You can also use key paths to follow relationships between objects.
Key-value observing (KVO). This protocol defines a mechanism through which one object can register with another object to be notified of changes to the value of a specified property.
Controller Objects Help You to Establish Bindings
In the Model-View-Controller design pattern, a controller object synchronizes changes in the presentation of data (view layer) with the objects that store that data (model layer), and it communicates changes in stored data to the view that presents the data. In this way a controller object acts as a mediator.
AppKit offers four types of controller objects that you can use unmodified to establish bindings in your application. These controller objects (each of which inherits from the abstract
NSController class) are available in the Interface Builder library.
NSObjectControllermanages a single model object.
NSArrayControllermanages an index-based collection of model objects and tracks the current selection.
NSTreeControllermanages a hierarchical collection of model objects and tracks the current selection.
NSUserDefaultsControllerprovides a convenient interface to the preferences system (that is, user defaults).
Technically, you don’t need
NSController objects to establish bindings; bindings can be made between any two objects, provided that they’re compliant with KVO and KVC . However, if you don’t use
NSController objects, you lose what they offer you, namely, placeholder and current-selection values and the ability to commit and discard pending changes.
Use Interface Builder to Establish Bindings
Another technology that is essential to establishing bindings during development is the Interface Builder application. The Bindings view of the application’s Inspector window lets you specify the objects and properties involved in each binding.
When developing an application, you first drag an
NSController object from the library to your nib file. Then, in the Bindings view, bind a model property to a property of the controller. Next, select a view in your nib file and bind an appropriate attribute of the view (for example,
data) to the model property via the controller property.