The basic concept of delegates is to delegate important decisions or information to some other object instance.
In most frameworks you use subclassing and override methods in order to hook into the application flow. It works but the drawbacks are many, for example:
- You can not change the decision maker without a complete new subclass.
- Without multiple inheritance you can only make decisions for one object (yourself).
There are four reasons why an object might want to call upon a delegate, and each of these four uses a keyword in the delegate method name to signal this. It's a naming convention only, but you should follow the pattern if you want to be a good citizen.
- Ask if something should happen. For example:
gestureRecognizer:
should
ReceiveTouch:
- Before something unavoidable is going to happen. For example:
application
Will
Terminate:
.
- After something has occured. For example:
accelerometer:
did
Accelerate:
- And to retrieve data, this is more a data source than a delegate, but the line between the two are fuzzy. The name do not contain a defined name, but should contain the named piece of data that is requested. For example:
tableView:
targetIndexPath
ForMoveFromRowAtIndexPath:toProposedIndexPath:
As a general rule the first argument to any delegate method should be the named object instance requesting delegation.