I’ve seen this question asked time and time again and it goes a little something like this. I have a custom class that I’ve made and when something happens I would like to be notified in another class. Well, you could simply use NSNotificationCenter for this but there is a much more elegant solution available and its called a delegate. So how do you make a delegate? Follow the code samples below and you should have your delegate ready to go within a few minutes.

We will be referencing 2 classes in this example. Lets call them MyViewController and MyCustomClass. MyViewController will be the class implementing the delegate and MyCustomClass will be the delegate. First lets look at the code and then we will go over the details.

MyCustomClass.h

 

MyCustomClass.m

 

MyViewController.h

 

MyViewController.m

 

The code listed above is very simple and very basic but should get you started with creating your own delegates. Now for the fun part. Lets go over this code a little, we will do this 1 file at a time starting with the MyCustomClass.h file.

MyCustomClass.h

In the first line we define the class MyCustomClass so that the delegate protocol can see and reference it.

In the second line we define a delegate protocol. This is followed by a delegate method which will be the method that we will have to implement in our MyViewController class. After this we end the protocol. We then go on to define the interface and use an NSObject subclass. You don’t have to use an NSObject this is just what was used for this example.

Next we define a property for the MyCustomClass class for the delegate. This property will be set in the MyViewController class. Most of the code in this header file should be pretty self explanatory. If you would like more information on protocols please refer to the Apple documentation. Moving right along lets look at the MyCustomClass.m file.

 

MyCustomClass.m

In the first 3 lines of code we import our header file, define the implementation for the MyCustomClass class and synthesize our delegate property.

Next we create a method. This can be named whatever you want and will be the method that this class will call when you want to send some type of notification, for instance if this class handles uploads to a server and you want to be notified in you MyViewController class when the uploads have finished, you would from this MyCustomClass call the somethingFinished method which would in turn call the delegate method that will be implemented in our MyViewController class.

 

MyViewController.h

The first line imports the MyCustomClass.h header. The next line defines an interface for this class and tells the class that we would like to be a delegate for the MyCustomClass class. We then create a property for the MyCustomClass class that we can then synthesize and implement in the MyViewController.m file

 

MyViewController.m

The only lines of code I’m going to explain here are the line of code in the viewDidLoad method and the delegate method at the bottom.

The line in the viewDidLoad method sets the delegate property of the MyCustomClass class to be the MyViewController meaning that we will receive delegate method calls.

The last line of code in our MyViewController.m file is the actual implementation of the delegate method that was created in the MyCustomClass class

 

Ok, that’s pretty much it, I hope that this has helped out some of you and that delegates make a little more sense at this point. If anyone has any questions, please feel free to comment.

Be Sociable, Share!
The following two tabs change content below.
I've been in the Geek category for as long as I can remember. I am into anything technology and Sci-Fi related. I like tinkering with electronics as well as writing applications for Mac & iPhone.

Leave a Reply

Post Navigation