Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / iOS

Creating Delegates in Objective-C

4.25/5 (3 votes)
14 Feb 2015CPOL2 min read 35.9K  
How to create delegates in Objective-C

Introduction

About a month ago, I created a gist here to give me a quick copy/paste solution for when I am creating delegates in Objective-C with UIViewController. While the sample code in the gist is targetted towards use with UIViewController, the use and implementation of delegates is no different.

Background

For some background information about delegates, you can review Apples documentation here. I would also recommend that you review the Apple documentation on protocols here.

Using the Code

First start by defining the protocol and for this situation I am going to list the methods that it needs to implement. I could if I wanted also define properties for the protocol but that is for another discussion.

Defining the Protocol

Open the header file where you want to define the protocol. In the following example, the protocol is also going to use a UIViewController class in one of its methods. To achieve this, I make sure that is defined before the protocol using @class followed by the name of the UIViewController.

After the protocol has been defined, it then needs to be used as property in whatever class that will take use of it. Since the property for the protocol could be of any class, then it will be marked as a type of id.

C++
#import <UIKit/UIkit.h>
@class SomeViewController;
 
@protocol SomeViewControllerDelegate <NSObject>
 
-(void)delegateMethodName: (SomeViewController *) controller;
 
@end
@interface SomeViewController : UIViewController
 
@property (nonatomic, weak) id<SomeViewControllerDelegate> delegate;
 
@end

As just a note, the methods defined in the protocol are marked as being optional. Basically, this means that whatever class chooses to implement the protocol does not need to implement all the methods defined. This behavior can be controlled with @optional and @required as part of the protocol definition.

The following is a basic example that demonstrates their use:

C++
#import <UIKit/UIkit.h>
@class SomeViewController;
 
@protocol SomeViewControllerDelegate <NSObject>

@optional
-(void)delegateMethodName: (SomeViewController *) controller;

@required
-(BOOL)delegateMethodReturningBool: (SomeViewController *) controller;
 
@end
@interface SomeViewController : UIViewController
 
@property (nonatomic, weak) id<SomeViewControllerDelegate> delegate;
 
@end

Using the Protocol

The following is a basic example that demonstrates how to call one of the methods that were defined in the protocol.

C++
- (void) someMethodToCallDelegate { 
   [self.delegate delegateMethodName:self]; 
}

In the following example is a demonstration that will also let you to test whether the method has been defined before you call it.

C++
- (void) someMethodToCallDelegate {
   if ([[self delegate] respondsToSelector:@selector(delegateMethodName:)]) {
      [self.delegate delegateMethodName:self]; 
   }
}

Implementing the Protocol

The following demonstrates the implementation of the protocol.

C++
#import "delegate.h"
 
@interface SomeDelegateUser ()
<SomeViewControllerDelegate>
@end
 
@implementation SomeDelegateUser
 
- (void) variousFoo {
   SomeViewController *controller = [[SomeViewController alloc] init];
   controller.delegate = self;
}
 
-(void)delegateMethodName: (SomeViewController *) controller {
   // handle the delegate being called here
}

-(BOOL)delegateMethodReturningBool: (SomeViewController *) controller {
   // handle the delegate being called here
   return YES;
}
 
@end

As always... happy coding!!!

History

  • 14-Feb-2015 - Initial release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)