Hi, all.
I usually read a word 'delegate' in the apple document and their library book. What exactly meaning about this word? Any special meaning in iPhone?
Thank you very much.
Hi, all.
I usually read a word 'delegate' in the apple document and their library book. What exactly meaning about this word? Any special meaning in iPhone?
Thank you very much.
Delegate is a object that controls the whole application. It shows the main window and will tell the app what to do when it is launched and closed. It is basically the command center of your app. The best way to use this is to call other controllers to display some NIBS. There really isn't anything special about a delegate in an iPhone app, but there are special methods that are called.
Delegate is a Design Pattern that Apple adopts heavily. In a nutshell, think of it like "I'm responsible for handling ...". Where ... is a notification, event, protocol, etc. For example, your AppDelegate
is responsible for your handling your App
setup, display, launch.
Please keep in mind I am over simplifying it. But I am sure someone can provide a much more detailed answer if you need.
A delegate is the same thing as a callback function in JS ( the exception being that delegates are type-safe.) For example, if you're doing Ajax in JS, you declare a callback function to be called when the Ajax call is finished. In this same fashion, you would declare a delegate function to be called, for example, when a song selection dialog is closed.
Delegates are a design pattern in object-oriented languages that allow an object to "call out" to unknown code to perform activities or calculations that that object cannot effectively do on its own. Let's say you have a class Dog:
@protocol DogDelegate;
@interface Dog : Wolf <Domesticated>
- (void)bark;
- (void)tiltHeadAdorably;
- (void)playWithToy: (Toy *)aToy;
@property (readonly) Toy *favoriteChewToy;
@property (readwrite, assign) id <DogDelegate> delegate; // "DELEGATE" PROPERTY DECLARED HERE
@end
The delegate object is generally supplied by the code that instantiates Dog
, and is called upon by that instance to do things that the dog itself can't do. For instance, consider this interface of the DogDelegate
protocol, which defines what the Dog
's delegate object is expected to do:
@protocol DogDelegate <NSObject>
@required - (void)letDogOut: (Dog *)aDog;
@required - (void)letDogIn: (Dog *)aDog;
@optional - (void)scratchDog: (Dog *)aDog forTimeInterval: (NSTimeInterval)duration;
@end
In this case, an instance of DogDelegate
is often the owner of the Dog
(and, in Objective-C, a delegate often owns an object, so this lines up nicely with the metaphor.) The dog, when it needs to go out for... dog activities... will ask its delegate to perform the -letDogOut:
method, placing the dog in the backyard. When done, it will ask its delegate to perform the -letDogIn:
method, bringing the dog back inside. When the dog wants affection, if its delegate is able to, it will ask the delegate to scratch it for some period of time using -scratchDog:forTimeInterval:
.