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
.
#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:
#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.
- (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.
- (void) someMethodToCallDelegate {
if ([[self delegate] respondsToSelector:@selector(delegateMethodName:)]) {
[self.delegate delegateMethodName:self];
}
}
Implementing the Protocol
The following demonstrates the implementation of the protocol.
#import "delegate.h"
@interface SomeDelegateUser ()
<SomeViewControllerDelegate>
@end
@implementation SomeDelegateUser
- (void) variousFoo {
SomeViewController *controller = [[SomeViewController alloc] init];
controller.delegate = self;
}
-(void)delegateMethodName: (SomeViewController *) controller {
}
-(BOOL)delegateMethodReturningBool: (SomeViewController *) controller {
return YES;
}
@end
As always... happy coding!!!
History
- 14-Feb-2015 - Initial release
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.