Apple offers a good overview of working with protocols in their Objective-C Programming Reference. However, sometimes a simple working example can go a long ways…
Introduction
Protocols can be helpful in a number of scenarios, a common usage is to define methods that are to be implemented by other classes. A familiar example is when using a tableview, your class implements the cellForRowAtIndexPath method which asks for cell content to insert into a table – the cellForRowAtIndexPath method is defined within the UITableViewDataSource protocol.Let’s walk through a very simple example of defining and adopting a protocol.
Protocol Definition
Here is an example of a protocol which includes one method, notice the instance variable delegate is of type id, as it will be unknown at compile time the type of class that will adopt this protocol.#import <Foundation/Foundation.h> @protocol ProcessDataDelegate <NSObject> @required - (void) processSuccessful: (BOOL)success; @end @interface ClassWithProtocol : NSObject { id <ProcessDataDelegate> delegate; } @property (retain) id delegate; -(void)startSomeProcess; @end |
Protocol Implementation
Inside the implementation section for the interface defined above we need to do two things at a minimum – first synthesize the delegate instance variable and second, call the method defined in the protocol as needed (more on that in a moment).Let’s look at a bare bones implementation of the ClassWithProtocol.m:
#import "ClassWithProtocol.h" @implementation ClassWithProtocol @synthesize delegate; - (void)processComplete { [[self delegate] processSuccessful:YES]; } -(void)startSomeProcess { [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(processComplete) userInfo:nil repeats:YES]; } @end |
In the calling class, the method defined in the protocol, processSuccessful, will be implemented and will be called from the object doing the processing, once it is complete.
For this example, inside the class where the protocol is defined, I have one method, startSomeProcess, which simply starts a timer and calls processComplete after 5 seconds. Inside processComplete the calling object will be notified through its delegate that the process is done.
Adopting the Protocol
To keep the example short, I am using the applicaton delegate as the class that adopts the protocol. Here is how the app delegate looks:#import <UIKit/UIKit.h> #import "ClassWithProtocol.h" @interface TestAppDelegate : NSObject <UIApplicationDelegate, ProcessDataDelegate> { UIWindow *window; ClassWithProtocol *protocolTest; } @property (nonatomic, retain) UIWindow *window; @end |
Here is the implementation of the app delegate and the required method for the protocol:
#import "TestAppDelegate.h" #import "ClassWithProtocol.h" @implementation TestAppDelegate @synthesize window; UITableViewDelegate - (void)processSuccessful:(BOOL)success; { NSLog(@"Process completed"); } - (void)applicationDidFinishLaunching:(UIApplication *)application { // Create and initialize the window window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; protocolTest = [[ClassWithProtocol alloc] init]; [protocolTest setDelegate:self]; [protocolTest startSomeProcess]; [window makeKeyAndVisible]; } - (void)dealloc { [window release]; [super dealloc]; } @end |
No comments:
Post a Comment