< Previous PageNext Page >

Bindings

Bindings are a Cocoa technology that you can use to synchronize the display and storage of data in an application. They are an important tool in the Cocoa toolbox for enabling communication between objects. The technology is an adaptation of both the Model-View-Controller and object modeling design patterns. (“The Model-View-Controller Design Pattern” introduced bindings in its discussion of controller objects.). It allows you to establish a mediated connection—a binding—between the attribute of a view object that displays a value and a model-object property that stores that value; when a change occurs in the value in one side of the connection, it is automatically reflected in the other. The controller object that mediates the connection provides additional support, including selection management, placeholder values, and sortable tables.

In this section:

How Bindings Work
How You Establish Bindings


How Bindings Work

BIndings inhabit the conceptual space defined by the Model-View-Controller (MVC) and object modeling design patterns. An MVC application assigns objects general roles and maintains separation between objects based on these roles. Objects can be view objects, model objects, or controller objects whose roles can be briefly stated as follows:

All objects, but most importantly model objects, have defining components or characteristics called properties. Properties can be of two sorts: attributes—values such as strings, scalars, and data structures—and relationships to other objects. Relationships can be of two sorts: one-to-one and one-to-many. They can also be bidirectional and reflexive. The objects of an application thus have various relationships with each other, and this web of objects is called an object graph. A property has an identifying name called a key. Using key paths—period-separated sequences of keys—one can traverse the relationships in an object graph to access the attributes of related objects.

The bindings technology makes use of this object model to establish bindings among the view, model, and controller objects of an application. With bindings you can extend the web of relationships from the object graph of model objects to the controller and view objects of an application. You can establish a binding between an attribute of a view object and a property of a model object (typically through a mediating property of a controller object). Any change in the displayed attribute value is automatically propagated through the binding to the property where the value is stored. And any internal change in the value of the property is communicated back to the view for display.

For example, Figure 5-6 shows a simplified set of bindings between the displayed values of a slider and a text field (attributes of those view objects) and the number attribute of a model object (MyObject) through the content property of a controller object. With these bindings established, if a user moves the slider, the change in value would be applied to the number attribute and communicated back to the text field for display.


Figure 5-6  Bindings between view, controller, and model objects

Bindings between view, controller, and model objects

The implementation of bindings rests on the enabling mechanisms of key-value coding, key-value observing, and key-value binding. See “Key-Value Mechanisms” for overviews of these mechanisms and their associated informal protocols. The discussion of the Observer pattern in “Cocoa Design Patterns” also describes key-value observing.

You can establish a binding between any two objects. The only requirement is that the objects comply with the conventions of key-value coding and key-value observing. However, you generally want to establish the binding through a mediating controller because such controller objects offer bindings-related services such as selection management, placeholder values, and the ability to commit or discard pending changes. Mediating controllers are instances of several NSController subclasses; they are available on the Interface Builder Controllers palette (see “How You Establish Bindings”, below). You can also create custom mediating-controller classes to acquire more specialized behavior. For a discussion of mediating controllers and NSController objects, see “Types of Cocoa Controller Objects” in “The Model-View-Controller Design Pattern” and the section on the Mediator pattern in “Cocoa Design Patterns”.

Further Reading: To learn more about the design patterns summarized above, see “The Model-View-Controller Design Pattern” and “Object Modeling”.

How You Establish Bindings

If the only custom classes of your application are model classes, the only requirement for establishing bindings is that those classes be compliant with key-value coding conventions for any properties you want to bind. If you are using a custom view or custom controller, you should also ensure that it is key-value observing compliant. See “Key-Value Mechanisms” for a summary of the requirements for compliance with both key-value coding and key-value observing.

Note: Most of the classes of the Cocoa frameworks are compliant with key-value coding and (where appropriate) key-value observing.

You can also establish bindings programmatically but for most situations you use the Interface Builder application to establish bindings. In Interface Builder, you start by dragging NSController objects from a palette into your nib file. Then you use the Bindings pane of the Info window to specify the relationships between the properties of the view, controller, and model objects of your application and the attributes you want bound.

Figure 5-7 gives an example of a binding. It shows the "Name" column of the top table view bound to the model attribute name through the property arrangedObjects of the Entrees controller (an NSArrayController object), which itself is bound to an array of model objects (not shown).


Figure 5-7  Establishing a binding in Interface Builder

Establishing a binding in Interface Builder

Further Reading: Read Cocoa Bindings Programming Topics to learn more about the bindings technology and how to use Interface Builder to establish bindings. Also see Key-Value Coding Programming Guide and Key-Value Observing Programming Guide for complete descriptions of these mechanisms.



< Previous PageNext Page >


© 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-12-20)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.