Show Menu

Looking for an iOS Developer?

Submit your 30 day Job Listing for FREE

To create your own custom delegate protocol, you need to modify the header file (.h) for the selected class to then add the @protocol declaration, a delegate @property, and declare the methods that delegates can implement:

Please note; this tutorial is written on the understanding that you have basic to moderate knowledge in the iOS Application Development language: Objective-C. If this is not the case then I suggest that you read Beginners Guide to Objective-C for Developers to further develop your knowledge and ensure you can successfully follow on with this tutorial.


// YourViewController.h:

#import 

@protocol YourProtocolName

@interface YourViewController: UIViewController

@property (nonatomic, weak) id delegate;

@end

@protocol YourProtocolName 
@required
-(void)requiredDelegateMethod;

@optional
-(void)optionalDelegateMethodOne;
-(void)optionalDelegateMethodTwo:(NSString *)withArgument;

@end // end of delegate protocol

Next, In the implementation file, anytime you want to call the delegate method, you first need to check and see if the delegate is set, and then if it responds to the selector. Once this is checked, then you can call the method, for this we are going to use an Objective-C if Statement:


if (self.delegate && [self.delegate respondsToSelector:@selector(optionalDelegateMethodOne)]) {
    [self.delegate optionalDelegateMethodOne];
}

Now for classes you want to conform to your new protocol, include the header file and delegate protocol in the @interface:


#include "YourViewController.h"  // needed to include the @delegate protocol info

@interface FavoritesViewController : UIViewController 

Finally, any required delegate methods must then be implemented in your class’s @implementation file.

Update from reddit user brainotomy:

I thought it worth while to explain that the first @protocol line in the header is only needed if the actual protocol comes after the @[email protected] code.
So this works as well


// YourViewController.h:

 #import 

 @protocol YourProtocolName 
 @required
 -(void)requiredDelegateMethod;

 @optional
 -(void)optionalDelegateMethodOne;
 -(void)optionalDelegateMethodTwo:(NSString *)withArgument;

 @end // end of delegate protocol

 @interface YourViewController: UIViewController

 @property (nonatomic, weak) id delegate;  

 @end

having issues?

We have a Questions and Answer section where you can ask your iOS Development questions to thousands of iOS Developers.

Ask Question

FREE Download!

Get your FREE Swift 2 Cheat Sheet and quick reference guide PDF download when you sign up to SwiftMonthly


Sharing is caring

If you enjoyed this tutorial, please help us and others by sharing using one of the social media buttons below.


Written by:

I work in the shadows. You can thank me by being a good soul.

Comments

There are currently: 3 Responses to “Quick Tip: Create your own Objective-C Delegate Protocol”. Leave your comment!

Simply add the delegate to your .h file
Ex :
@interface viewControllerName : UIViewController


Nice tutorial works great.


This works great. If you don’t want to or cannot share your view controller’s header, put the protocol definition in its own or a different header file.


Leave a Reply

Would you like to sign up to the mailing list by our sister site: SwiftMonthly?