Simple delegate tutorial for ios development

Hello everyone,

I’m writing this tutorial as an introduction to the delegate design pattern in IOS development. This tutorial is based around a very simple example I’ve put together as an iphone project.

As I was teaching myself iphone development I avoided properly learning using delegate functions other than getting a table view to work which was a real drag with my projects because creating custom delegate methods is a powerful tool that I should have been using all along.

Prerequisites for this tutorial are a basic understanding of UIKIt and communication between view controllers.

The example project which you can download at this link: Delegate Example Project

Purpose of this example: This example is a bare bones iphone project which contains two view controllers where the root viewcontroller (DelegateExampleViewcontroller) is meant to display a user entered number and a view controller which will be presented as a modal view controller (EnterAmountViewController) and the value entered by the user will be sent to the root view controller through a delegate method. Just running the project will explain better than I can in this paragraph.

Lets get started:

Open up EnterAmountViewController.h


//delegate to return amount entered by the user
@protocol EnterAmountDelegate 
@optional
-(void)amountEntered:(NSInteger)amount;

@end

This is the delegate declaration. For our delegate we only have one method which will be sending back to our root view controller an NSInteger which we will get from the user.

NOTE: we extend the protocal to be of type “NSObject” which helps with autocomplete

ALSO: the @optional qualifier means that any object which is declared to be a EnterAmountDelegate can implement that function but if the @required is used in its place xcode will require that the delegate implement that function.

@interface EnterAmountViewController : UIViewController {

    IBOutlet UITextField *amountTextField;

}

-(IBAction)cancelPressed;
-(IBAction)savePressed;

@property(nonatomic,assign)id delegate;

@end

As you can see we declare id which is a reference to the defined delegate. We need to declare this as a property so the root view controller can access it.

Next open up EnterAmountViewController.m and specifically look at this function:

-(IBAction)savePressed
{
    //Is anyone listening
    if([delegate respondsToSelector:@selector(amountEntered:)])
    {
        //send the delegate function with the amount entered by the user
        [delegate amountEntered:[amountTextField.text intValue]];
    }

    [self dismissModalViewControllerAnimated:YES];
}

This function is connected to the save button on the toolbar which would be when the user is done entering their number(which doesn’t really mean anything). This is the point where we’re gonna call the delegate function which we declared in the .h. Before we call the function we first check if anyone is listening with if([delegate respondsToSelector:@selector(amountEntered:)])

If someone is listening then we call amountEntered with the integer value of our text field.

Next open up DelegateExampleViewController.h

The one line to pay attention to is @interface DelegateExampleViewController : UIViewController {

where the the view controller is being declared as an “EnterAmountDelegate” which is saying that it will implement all required delegate functions and may implement some or all of the optional delegate functions. In our example we only have one optional delegate function.

Finally up DelegateExampleViewController.m

#pragma mark EnterNumbe Delegate function

//display the amount in the text field
-(void)amountEntered:(NSInteger)amount
{
    amountLabel.text = [NSString stringWithFormat:@"%i" , amount];
}

This is delegate function being implemented in our view controller. This function will be called from EnterAmountViewController and we know it will contain our user input which we care about. All we are doing is taking that value and displaying it as a string in our UILabel.

Thats it. In this tutorial we’ve gone over a basic example of using a delegate protocal to communicate between two view controllers. This probably could have been accomplished using notifications or another hacky method but by using delegates we have made the view controller much more reusable and organized.

I hope this tutorial has helped and if it has I would appreciate a comment or send me an email: Email Me

Again here is the sample iphone project. Delegate Example Project

-Ryan

You Are Your Own Gym