Welcome to Day 6 in the 31 Days of iOS.  In yesterday’s article, we showed a few different ways to programmatically display a second view controller in our app.  Today we’re going to look at a very important concept when developing for iOS:  the Delegation pattern.  Using delegates is very common in Objective-C development.  It can also be used in other programming languages, however, in Objective-C it’s very easy to see it in action due to the use of the keyword delegate.  You can download the code we’re going to start with (from yesterday’s article) here. If you’d like to follow along with the completed code, you can access it here.

The Delegate Pattern

The idea behind delegates is that instead of class A executing some code, it tells it’s delegate to execute that code.  The delegate is tied to another class (let’s call it class B).  In order to facilitate this, class A creates something called a protocol.  This protocol has a list of methods in it (with no definition).  Class A then has an instance of the protocol as a property.  Class B has to implement the protocol defined in class A.  Lastly, when class A is created, it’s delegate property is set to class B.  That may be a little confusing so we’ll look at some actual code for this next.

Creating the Protocol and Delegate

We’re going to add a protocol and delegate to the SecondViewController class:

Notice the @class line at the top.   This is necessary for us to reference the SecondVieController class inside of our protocol.  If you didn’t need to reference the class in the protocol (as a parameter like we are) then this @class line wouldn’t be necessary. 

Using the Delegate

Flipping over to SecondViewController.m, we first need to synthesize the delegate object.  Then we’re going to send the delegate the doSomething message when a button is tapped:

As of right now, the delegate hasn’t been set to anything. If you run your app and tap the associated button, it won’t error out.  Nothing “bad” will happen.  However, this obviously isn’t what we want.  Let’s look next at how to connect the delegate to something.

Implementing the Protocol

The first step we have to do is open up the class we want to handle the action when the delegate is called.  For this sample, we’re going to open ViewController.h.  First you’ll need to import the SecondViewController.h file and make this class implement the SecondViewControllerDelegate protocol:

Those are the only changes needed to the .h file.  Switching to the ViewController.m file, we need to make two changes.  First, when we instantiate and show the SecondViewController class, we need to set it’s delegate property:

The important line here is the one where .delegate is set to self.  This tells SecondViewController that whenever a message it sent to it’s delegate property, it should be sent to the ViewController instance we’re in.  Now we just need to implement the method from the protocol:

In this case, we’re dismissing the modal.  Note that this will only work when the second view is presented as a modal (by tapping the Show New View button).  If you wanted to dismiss a view displayed on the NavigationController, you’d need to replace the call to dismissModalViewControllerAnimated with a call to self’s navigationController property’s popViewControllerAnimated method.

Conclusion

Today we took a look at the delegation pattern in Objective-C.  You’ll find uses for this as you program your own classes, but you’ll also see places where you need to use this pattern to interact with the UIKit and the Foundation framework.  For example, if you use the UIImagePickerController to allow the user to select a picture from the device’s gallery, you handle the call back by implementing the UIImagePickerControllerDelegate.  You can download the final code from today here.


Chris Risner