This is copied from the iOS Apprentice tutorial on raywenderlich.com but I wanted to put it where I could easily access it. These are the basic steps for setting up a delegate pattern between two objects, where object A is the delegate for object B, and object B will send messages back to A.
- Define a delegate
protocol
for object B - Give object B an optional
delegate
variable. This variable should be weak - Make object B send messages to its delegate when something interesting happens, such as the user pressing the Cancel or Done buttons, or when it needs a piece of information. You write
delegate?.methodName(self, ...)
- Make object A conform to the delegate protocol. It should put the name of the protocol in its
class
line and implement the methods from the protocol. - Tell object B the object A is now its delegate