User Tools

Site Tools


ios-labs-s14:basics-delegation

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
ios-labs-s14:basics-delegation [2014/02/15 17:59]
mbarboi created
ios-labs-s14:basics-delegation [2014/02/26 13:20]
mbarboi
Line 1: Line 1:
 =====Delegation===== =====Delegation=====
-delegate is an object that acts on behalf of, or in coordination with, another object ​when that object encounters an event in a program. The delegating object is often a responder object—that is, an object inheriting from NSResponder in AppKit or UIResponder in UIKit—that is responding ​to a user event. The delegate is an object that is delegated control of the user interface for that event, or is at least asked to interpret the event in an application-specific manner.+design pattern wherein ​an object that needs details about its functionality delegates ​another object to handle those details.
  
-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 frameworkfor exampleresponds 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.+Allows ​objects to exchange notificationsdata, and implementation details without making ​the two objects **dependent**
  
-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 itThe 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.+Two parts to delegation
 +  -Set an object ​as the delegate ​of another ​using "other.delegate ​= object"​ or through InterfaceBuilder 
 +  ​-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. ​
 +
 +===Verbose===
 +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