A delegate is a pointer to an object with a set of methods the delegate-holder knows how to call. In other words, it’s a mechanism to enable specific callbacks from a later-created object.
A good example is UIAlertView
. You create a UIAlertView
object to show a short message box to users, possibly giving them a choice with two buttons like “OK” and “Cancel”. The UIAlertView
needs a way to call you back, but it has no information of which object to call back and what method to call.
To solve this problem, you can send your self
pointer to UIAlertView
as a delegate object, and in exchange you agree (by declaring the UIAlertViewDelegate
in your object’s header file) to implement some methods that UIAlertView
can call, such as alertView:clickedButtonAtIndex:
.
Check out this post for a quick high-level intro to the delegate design pattern and other callback techniques.
References:
- UIAlertView class reference
- UIAlertViewDelegate class reference
- Apple’s guide to Delegates and Data sources