Can anyone explain the DELEGATE definition in iphone sdk with real time scenario.
Sorry for my poor question here.
Thanks in advance.
Can anyone explain the DELEGATE definition in iphone sdk with real time scenario.
Sorry for my poor question here.
Thanks in advance.
A delegate is an object that will respond to pre-chosen selectors (function calls) at some point in the future.
Suppose I am loading a URL asynchronously (in the background), using an object of class FancyAsynchronousURLGetter. Since it is running in the background, I want to be able to go and do something else while the url is loading, and then be notified when it's ready. By using a delegate on the FancyAsynchronousURLGetter and writing the appropriate code, I can specify an object with a particular selector that will be called when the FancyAsynchronousURLGetter is finished. Something like this:
- (void)loadView
{
...
FancyAsynchronousURLGetter* getter = [[FancyAsynchronousURLGetter alloc] initWithURL:url];
[getter setDelegate:self];
/*
getter will call either
- (void)fancyAsynchronousURLGetterLoadSucceeded:(FancyAsynchronousURLGetter*)g
or
- (void)fancyAsynchronousURLGetterLoadFailed:(FancyAsynchronousURLGetter*)g
on its delegate, depending on whether load succeeded or failed
*/
[getter start];
...
}
- (void)fancyAsynchronousURLGetterLoadSucceeded:(FancyAsynchronousURLGetter*)g
{
NSLog(@"Load succeeded.");
}
- (void)fancyAsynchronousURLGetterLoadFailed:(FancyAsynchronousURLGetter*)g
{
NSLog(@"Load failed.");
}
and in the FancyAsynchronousURLGetter itself:
- (void)start
{
[self performSelectorInBackground:@selector(fetchURL) withObject:nil];
}
- (void))fetchURL
{
Fetch the URL synchronously
if ( success )
[delegate fancyAsynchronousURLGetterLoadSucceeded:self]; // note: probably want to call this on the main thread
else
[delegate fancyAsynchronousURLGetterLoadFailed:self];
}
Here is a sample code to understand.
Protocol Definition
#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
#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
To get full idea, visit here...
Short:Delegation is a one to one relationship between two objects that allows one to call methods on another, and it lets the callee do custom implementation of that method.
Delegation lets you write custom implement customized implementation of methods.
For instance UITableView's Delegate and datasource (which is technically same as delegate) allows the UITableView to let other classes important tasks such as creation of cells to display in a tableview and responding to events (e.g. tapping a cell in the tableview).
To Use delegation first you need to define a protocol:
@protocol protocolNameHere
- (void) sampleMethodHere;
@optional
- (void) implementingThisMethodIsOptional;
@end
then you have to add the name of the protocol in "<>" in front of the header file of the class you want to be delegate. that class now conforms to that protocol. you need to implement the method inside this class.
If there are more than one delegates then you separate them with commas, e.g.
Delegate isn't something depend on language or platform you use, it is kind of design pattern. I suggest you to read Delegation pattern for basic understanding. After that I think you can search fro further understanding.