User Tools

Site Tools



A design pattern wherein an object that needs details about its functionality delegates another object to handle those details.

Allows objects to exchange notifications, data, and implementation details without making the two objects dependent.

Two parts to delegation:

  1. Set an object as the delegate of another using “other.delegate = object” or through InterfaceBuilder
  2. Implement delegate methods in delegate object

The act of setting a delegate fills a pointer in the object that needs a delegate. When this object needs details about its functionality, it calls known methods on the delegate object.

The delegate object must implement these methods and provide custom details. If a delegate method is required but not implemented in a delegate, app crashes.

Information about delegate methods is stored in a protocol, which is a set of method declarations stored in the header file of the object that needs the delegation. Each method may be required or optional.


A delegate is an object that acts on behalf of, or in coordination with, another object when that object encounters an event in a program.

To better appreciate the value of delegation, it helps to consider an off-the-shelf Cocoa object such as a text field (an instance of NSTextField or UITextField) or a table view (an instance of NSTableView or UITableView ). These objects are designed to fulfill a specific role in a generic fashion; a window object in the AppKit framework, for example, responds to mouse manipulations of its controls and handles such things as closing, resizing, and moving the physical window. This restricted and generic behavior necessarily limits what the object can know about how an event affects (or will affect) something elsewhere in the application, especially when the affected behavior is specific to your application. Delegation provides a way for your custom object to communicate application-specific behavior to the off-the-shelf object.

The programming mechanism of delegation gives objects a chance to coordinate their appearance and state with changes occurring elsewhere in a program, changes usually brought about by user actions. More importantly, delegation makes it possible for one object to alter the behavior of another object without the need to inherit from it. The delegate is almost always one of your custom objects, and by definition it incorporates application-specific logic that the generic and delegating object cannot possibly know itself.

ios-labs-s14/basics-delegation.txt · Last modified: 2014/02/26 13:20 by mbarboi