A delegate (which means representative) is a class('s object) that wants to also represent (inherit from) another unrelated class. The delegate object is able to inherit from an otherwise unrelated class by "conforming to" (having some implementations for) the unrelated class's required protocol methods, which indicate to the foreign class that this object is now capable of volleying its most basic information appropriately.
You use inheritance when you want related classes to share methods. You use delegates when you want unrelated classes to share methods. The delegate approach lets an object from one class inherit methods from an otherwise unrelated class. The "delegate object", or "representative object", is the object that is inheriting from a foreign class; it is designated as a representative, a delegate, of that remote class, so that when you assign an object as a delegate to a class, you are giving it permission to represent that class also, even though it does not normally inherit from that class. (If it inherited from the class, then you wouldn't need to set it as a delegate of that class; it would already have access to the class's methods. But you want this unrelated object to inherit some methods from the class, representing that class, and returning some information back to its sender class, so you make it a delegate representative of that class even though it inherits from a different set of classes.) This essentially allows one unrelated class to inherit from another unrelated class, but with a minimum of complications to its basic line of inheritance.
You use the delegate system when you want an object to execute code from a separate class. For example, as described here, when you hit Enter in a text field, the text field doesn't really know what to do with that information. What it does is looks to its delegate object's class (such as the document's window controller or the document) and uses that class's relevant method which conforms to the text field's respective method found within its text field delegate protocol, something like textFieldShouldReturn. So in this case, you set the window controller or the document as the delegate of the text field, because the text field needs that class to represent the information it was given.
Class A needs a method from Class B, but Class A does not inherit from Class B. First, tell the compiler that Class A conforms to the Class B protocol:
@interface ClassA : NSObject <ClassBDelegateProtocol>
In the Apple Documentation, each class reference shows at the top, "Inherits from" and "Conforms to". For example, NSDocumentController
inherits from NSObject
and conforms to NSUserInterfaceValidations
, NSCoding
, and NSObject(NSObject)
. The conformance to NSCoding
is from the interface declaration in NSDocumentController.h
@interface NSDocumentController : NSObject <NSCoding>
and to NSUserInterfaceValidations
from the method declaration in NSDocumentController.h
-(BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)item;
along with the #import
#import <AppKit/NSUserInterfaceValidation.h>
So whereas NSDocumentController
inherits from NSObject
, it wants some help also from NSCoding
and NSUserInterfaceValidation
. And it gets help from these foreign classes by conforming to their protocol methods, defining itself as conforming to those methods, and importing any necessary header files to use those methods.