Click here to Skip to main content
15,895,746 members
Articles / Mobile Apps / iOS
Tip/Trick

Creating Delegates in Objective-C

Rate me:
Please Sign up or sign in to vote.
4.25/5 (3 votes)
14 Feb 2015CPOL2 min read 35.6K   2   2
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)


Written By
Software Developer
United States United States
I am software developer with over 20 years of professional experience. I have been employed as a software developer since the early 90′s back when Microsoft’s Windows 3.1x was gaining popularity and IBM’s OS/2 was the predominant leader in 32-bit PC based Operating Systems.

Prior to choosing this as my profession I had studied architecture and then later Electrical and Mechanical engineering in college. As a young kid growing up I always played with computers, my first computer was a TRS-80 that I would spend countless hours writing programs for, I never really thought of programming as a profession. The story goes that in my final year of college I took a C/C++ programming class and had so much fun working on the various projects that my professor told me something that changed everything.

“You know they pay people to do stuff like this for a living?” – Professor Bolman

Check out my blog here.

My current and ever evolving projects:

jqAlert javascript alerts done right for those using jQueryUI.
DooScrib Doodle and scribble pad written in javascript for use with HTML5 Canvas.

Comments and Discussions

 
QuestionSome remarks Pin
KarstenK17-Feb-15 2:55
mveKarstenK17-Feb-15 2:55 
AnswerRe: Some remarks Pin
Dennis E White17-Feb-15 4:16
professionalDennis E White17-Feb-15 4:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.