Friday, 19 October 2012

delegates in Objective C

What is a Delegate?
Technically speaking, a delegate is just an object that has been assigned by another object as the object responsible for handling events.

An example with real time situation:
A xyz office has some 5 counters, you will enter into office go to counter 1 and submit the some details etc. And the counter 1 will tell you to wait for some time and move to counter 3 to finish to get your work complete. In this case counter 3 is your delegate which handles your events, which is referred by counter 1. Here the same counter 1 may also be the delegate, to handle your events.

Why Delegate is required?
In one word we can define delegate as “Call-back”. Means after performing some task, who, why and how will handle the events.

What is the use of this Delegate?
While building any applications or projects delegates play a very important roles. In some situations some tasks should be performed out of the area and after finishing we should get to know the results, whether task is achieved or in progress or stopped. These statuses can be accessed by using the delegates.

Predefined Example:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@“Hello” message:@“This is an alert view” delegate:self cancelButtonTitle:@“OK” otherButtonTitles:nil];

The initializer of the UIAlertView class includes a parameter called the delegate. Setting this parameter to self means that the current object is responsible for handling all the events fired by this instance of the UIAlertView class. If you don’t need to handle events fired by this instance, you can simply set it to nil:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@“Hello”message:@“This is an alert view” delegate:nil cancelButtonTitle:@“OK” otherButtonTitles:nil];

If you have multiple buttons on the alert view and want to know which button was tapped, you need to handle the methods defined in the UIAlertViewDelegate protocol. You can either implement it in the same class in which the UIAlertView class was instantiated or create a new class to implement the method, like this:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog([NSString stringWithFormat:@“%d”, buttonIndex]);
}

To ensure that the alert view knows where to look for the method, create an instance of SomeClass and then set it as the delegate:

SomeClass *myDelegate = [[SomeClass alloc] init];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@“Hello” message:@“This is an alert view” delegate:myDelegate cancelButtonTitle:@“OK” otherButtonTitles:@“Option 1”, @“Option 2”, nil];
[alert show];


How to create our own delegates?
To create your own delegate, first you need to create a protocol and declare the necessary methods, without implementing. And then implement this protocol into your header class where you want to implement the delegate or delegate methods.

A protocol must be declared as below:

@protocol ServiceResponceDelegate <NSObject>

- (void) serviceDidFailWithRequestType:(NSString*)error;
- (void) serviceDidFinishedSucessfully:(NSString*)success;

@end

This is the service class where some task should be done. It shows how to define delegate and how to set the delegate. In the implementation class after the task is completed the delegate's the methods are called.

@interface ServiceClass : NSObject
{
id <ServiceResponceDelegate> _delegate;
}

- (void) setDelegate:(id)delegate;
- (void) someTask;

@end


@implementation ServiceClass

- (void) setDelegate:(id)delegate
{
_delegate = delegate;
}

- (void) someTask
{
...
...
...
perform task
...
...
...
if (!success)
{
[_delegate serviceDidFailWithRequestType:@”task failed”];
}
else
{
[_delegate serviceDidFinishedSucessfully:@”task success”];
}
}
@end

This is the main view class from where the service class is called by setting the delegate to itself. And also the protocol is implemented in the header class.

@interface viewController: UIViewController <ServiceResponceDelegate>
{
ServiceClass* _service;
}

- (void) go;

@end



@implementation viewController

//
//some methods
//

- (void) go
{
_service = [[ServiceClass alloc] init];
[_service setDelegate:self];
[_service someTask];
}


That's it, and by implementing delegate methods in this class, control will come back once the operation/task is done.
If you liked this tutorial or found something wrong with it please let me know!
I would love to hear your experience with it.

No comments:

Post a Comment