views:

132

answers:

4

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.

A: 

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.

Conceited Code
thank you for your reply. And I would like to ask what is 'NIBS'?
Questions
You're thinking of the application delegate, which is a particular delegate for a particular object, but is not the end-all and be-all of delegates; they're used throughout Cocoa and various other Objective-C frameworks. (Nibs are .NIB files; your interface files from Interface Builder.)
Jonathan Grynspan
A NIB is a file with the extension xib and they are views for your app. @jonathon - he's asking bout delegates in iPhone applications...
Conceited Code
And delegates in iPhone applications are the same as on the Mac, specific classes and protocols notwithstanding. :) The application delegate is but one of many delegates in a reasonably complex Objective-C application (on either iOS or Mac OS X,) though of course it is a very important delegate.
Jonathan Grynspan
Oh... Im sorry. You are correct. I forgot about that.
Conceited Code
Questions
+3  A: 

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.

Jason McCreary
A: 

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.

Mike Sherov
+2  A: 

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:.

Jonathan Grynspan
Thank you for your reply. It is very detail and useful. And can I realize that it is a control of a class?
Questions
I'm not sure what you mean by "control of a class." If you mean "can I create the delegate myself, and write its code?" then yes--that's what they're for. If you mean "can I choose which object is the delegate of another object?", then yes, and by default objects don't *have* a delegate set, so you *must* do this. If you mean "can a delegate be an instance of `UIControl/NSControl`?" then yes, again, though Apple's classes don't generally implement delegate methods for other classes.
Jonathan Grynspan
thank you very much. I have the better understanding now.
Questions