Using custom delegates in Objective-C
On Stack Overflow there has been some interest in how to use the Delegate design pattern in Objective-C. Of course, the first step in any search should be to read Apple's documentation, but many people seem not to want to read the whole thing. Trust me, folks, it is worth it. But Apple's documentation on creating delegates doesn't use protocols, which are such an amazing and useful part of the Objective-C language.
That being said, I'd like to give a short demonstration of how to create a class with a delegate. For the purposes of this tutorial, we'll call our class
JSTutorial. The interface to
JSTutorial starts out as the following:
Now, we need to modify this interface to include a delegate protocol:
The implementation for
JSTutorial might look like this:
Finally, the class that implements
JSTutorialDelegate might have its interface declared as follows:
SomeClass should implement
tutorialDidFinish:, but it is optional.
I hope that this has helped those who were struggling with the delegate design pattern.