Last Updated: February 25, 2016
·
414
· calvinx

iOS Delegation

Delegation is one of the most confusing parts of iOS development for people coming from the more contemporary languages.

But once you understand how delegation design pattern works in iOS, you are on your way to a lot of fun experimentation!

(1) Define a delegate @protocol for object B being called.
(2) Give object B a property for that delegate protocol and synthesize it accordingly
(3) Make object B send messages to its delegate when something interesting happens, such as the user hits a Cancel/Done buttons, or when it needs a piece of information.
(4) Make sure that the original object A conform to the delegate protocol. It should put the name of the protocol in its @interface line and implement the (required) methods from the protocol.
(5) Tell object B that it is now a delegate of object A via something like controller.delegate = self in object A's implementation so B knows about A when B is instantiated.