Monday, 8 October 2012

Delegate & Protocols: Creating Custom Delegate Methods in Objective C

What is Delegate :

A Delegate is an object that usually reacts to some event in another object and can affect how another object behaves.Delegates are used to delegate tasks of objects to their owner.A good reason for this is it makes it easier to use composition instead of inheritance.Delegates are a reference to an object that confirm to a specified protocol.So,you can guarantee it will implement the required methods.

One of the best examples (if we're talking iPhone) is theUITableViewDelegate, which is called to do certain things when certain table-related events occur.

Here,two things one is "dataSource" & "delegate".I am not going to describe about "datasource".When you use "mytableView.delegate = self" it will execute delegate method of UITableView that you define in your class.Here,you can find all the delegate method of UITableView (Link).

If we comments out this then it will not execute.

Now I am going to create an custom delegate method.Here,I create a project named "CustomDelegateMthod" which has two class 1.MainViewController and 2.DelegateMethod

First we need to create a protocol.

Protocols declare methods that can be implemented by any class. Protocols are useful in at least three situations:
  • To declare methods that others are expected to implement
  • To declare the interface to an object while concealing its class
  • To capture similarities among classes that are not hierarchically related

We can create more than one method as delegate method.Now, we need to create an identifier of our new protocol.

and then @synthesize it.

Now , we are ready to use this protocol.

At MainViewController.h I declare "DelegateMethod" type variable named "_delegate" and some UIL
abel to understand what is going to happen.

And MainViewController.m it is same as we use other buil in delegated method in objective c like UITableViewController etc.

Here,you will find two more -(IBAction).
When you click  -(IBAction)withDelegateButtonPressed:(id)sender it will call a method of DelegateMethod class.

And the method that we call from our MainViewController.m define as follows

Here,we modified the string that we send from MainViewController.m

We check here, whether methodWhereDelegateResponse: this method is declare on MainViewController.m. It is for safety of our code.And then it will hit in MainViewController.m class in  methodWhereDelegateResponse: and change the label value.

The main concept is here that we need to clear.I am trying to describe it one line.That is,we call a method of other class and it automatically hit a method of the main class (from where we send request).

Here, i use a image that i got from this Link that can make you understand clearly about Delegate.

Why we use It?

A delegate is merely a design pattern in Objective-C. Simply put, it provides a way for objects to "listen" for interesting things happening in your object and act on them without your object knowing anything about the object(s) listening. Delegates are used heavily in the Cocoa environment. Most often you will see delegates used to trigger methods on a UIViewController, although they can be implemented on anything.

After you suceessfully implement this code you will see this kind of out put

Download Code you can download whole project from this link.

and some other blogs.


  1. Nice Article Shohrab. This tutorial is about how to create custom delegate in objective c. If someone have interest on it, he will not find your tutorial! Because your title is: "Delegate And Protocols : A way to connect between classes"

    You can change the title, so that someone can easily find your tutorial.