Control object

A control is a type of view in a user interface that sends a message to another object when a user manipulates it in a certain way, such as tapping a button or dragging a slider. A control is the agent in the target-action model. A control (or, in OS X, the control’s cell) stores the information necessary for sending the message: a reference to the object to receive the message (the target) and a selector that identifies the method to invoke on the target (the action). When a user manipulates the control in a specific way, it sends a message to the application object, which then forwards the action message to the target.

Art/target_action.jpg

The abstract base classes for controls are UIControl, in the UIKit framework, and NSControl, in the AppKit framework. All controls are instances of concrete subclasses of these base classes. Common types of controls in these frameworks are buttons, sliders, and text fields. UIKit and AppKit also have controls that are specific to their platforms, for example, page controls (UIKit) and color wells (OS X).

In UIKit, Control Events Determine When Action Messages are Sent

Control events are an aspect of the UIKit framework’s design for controls. In UIKit, a target and an action selector determine the form of an action message, but one or more control events—also associated with the control—determine when the message is sent. A control event is a enum constant that represents the behavior of a touch (for example, UIControlEventTouchUpInside), a phase of an editing session (for example, UIControlEventEditingDidEnd), or a change in value (UIControlEventValueChanged). You may associate multiple control events with a control, and if the action represented by any one of these constants occurs, the control sends the action message to the target.

Some controls require that a certain control event be set. For example, UISwitch objects only send their action message when the UIControlEventValueChanged control event is satisfied.

In AppKit, Controls Can Have One or More Cells

Most controls in the AppKit framework have one or more cell objects associated with them. A cell is an instance of a class that inherits, directly or indirectly, from NSCell. Its main role is to store the target object and action selector for its control. Even though a cell is not a view, it can draw itself and respond to events, but only when instructed by its control. In the control-cell architecture, a control is the fronting view for its cell (or cells); it manages the behavior of the cell and composes and sends an action message when its distinctive user event occurs.

A few controls in the AppKit have multiple cells. For example, NSMatrix objects are controls that contain rows or matrices of cells of the same or different type. Each cell can have its own target and action selector. A few controls in AppKit store their target and action information themselves, and do not use a cell.

Prerequisite Articles

Related Articles

Definitive Discussion

Sample Code Projects