Using Bindings with Pop-Up Menus
Author: Mark Szymczyk
Last Update: December 8, 2005
Bindings are a Cocoa technology that simplifies the process of keeping data and user interface elements in sync. Most articles on bindings use a table view as an example. This article shows you how to use bindings with a pop-up menu.
A bedrock of object-oriented programming is the Model-View-Controller (MVC) design pattern. Model objects store data. View objects are user interface elements. Controller objects keep the model and view objects synchronized. Before bindings you had to write a lot of code for controller objects. Apple introduced bindings to Cocoa in Mac OS X 10.3 to reduce the amount of code you have to write.
The bindings technology has two components. The first component is a series of controller classes that manage one or more model objects. There are five controller classes, which you can find in Interface Builder’s Controllers Palette.
- Object controllers manage one object.
- Array controllers manage multiple objects.
- User defaults controllers manage user preferences.
- Managed object contexts work with Cocoa’s Core Data framework, which lets you create model objects without writing code.
- Tree controllers manage a tree of objects.
The second component of the bindings technology consists of the bindings themselves. Each user interface element has its own set of bindings, which you can see in Interface Builder by selecting an element and pressing Cmd-4. A binding connects one aspect of the user interface element with a controller or model object. The binding connects the view and the data.
Next (The Example Program)