Controller object

A controller object acts as a coordinator or as an intermediary between one or more view objects and one or more model objects. In the Model-View-Controller design pattern, a controller object (or, simply, a controller) interprets user actions and intentions made in view objects—such as when the user taps or clicks a button or enters text in a text field—and communicates new or changed data to the model objects. When model objects change—for example, the user opens a document stored in the file system—it communicates that new model data to the view objects so that they can display it. Controllers are thus the conduit through which view objects learn about changes in model objects and vice versa. Controller objects can also set up and coordinate tasks for an application and manage the life cycles of other objects. The Cocoa frameworks offer three main controller types: coordinating controllers, view controllers (on iOS), and mediating controllers (on OS X).

Art/controller_object.jpg

Coordinating Controllers

Coordinating controllers oversee and manage the functioning of an entire application, or part of one. They are often the places where application-specific logic is injected into the application. A coordinating controller fulfills a variety of functions, including:

  • Responding to delegation messages and observing notifications

  • Responding to action messages (which are are sent by controls such as buttons when users tap or click them)

  • Establishing connections between objects and performing other setup tasks, such as when the application launches

  • Managing the life cycle of “owned” objects

A coordinating controller is often an instance of a custom subclass of NSObject. In OS X, if a Cocoa application takes advantage of the document architecture, the coordinating controller is often an NSWindowController or NSDocumentController object. In iOS applications, view controllers often subsume the role of coordinating controller.

View Controllers

The UIKit and AppKit frameworks both provide view controller classes (for iOS and OS X, respectively) but these classes have different characteristics. In AppKit, a view controller is an instance of a custom subclass of the NSViewController class. The view controller owns a view archived in a nib file, and that view represents a data object. The view controller manages connections and updates to subviews of its view.

In UIKit, a view controller manages a view displaying a screenful of content; it keeps a reference to this view and may create or load it from a nib file. The controller manages the presentation of this view and the transition to any subsequent view in the app. (In most cases, the next view slides in from the right.) The navigation bar and the tab bar, and all their associated presentation behavior, are managed and implemented by view controller objects. View controllers can also display modal views, respond to low-memory warnings, and rotate views when the orientation changes.

A view controller in iOS is an instance of a subclass of UIViewController. The UIKit provides several special-purpose subclasses of UIViewController, such as UITableViewController. You must extend the framework view-controller classes to have the controller mediate the data between models and views. View controllers are typically the delegate or data source objects for many types of framework objects.

Mediating Controllers (OS X)

A mediating controller facilitates the flow of data between view objects and model objects. When users change a value displayed in a view object, the mediating controller automatically communicates the new value to a model object for storage; and when a property of a model changes its value, the mediating controller ensures that the appropriate view object displays the changed value. Unlike other types of controller objects, they are highly reusable. For these and other reasons, mediating controllers are a central component of the Cocoa bindings technology. You drag a mediating controller from the Interface Builder library and then configure these objects to establish bindings between the controller and its view objects and its model objects. A mediating controller is typically an instance of a concrete subclass of the abstract NSController class.

Prerequisite Articles

Definitive Discussion