in the 开发者_JAVA技巧chatty app if found this declaration , but i couldn't find an explanation for it anywhere.
id delegate
where RoomDelegate is a class.What is happening here?
Delegates are a common way of communicating between classes. For example, if you want to be notified when an object has finished a certain activity, you can register your class as a delegate for that object and as long as your class implements the required delegate method(s), these will be automatically called.
In this instance, the "id delegate" is just an instance variable that the class in question will use to store a reference to the delegate object. (i.e.: In our example, the object that wants to be informed when a certain activity is complete/event has occurred.)
If you look towards the bottom of the interface file, you'll most likely see a definition of the required XXXDelegate protocol - this is used to define the optional and required methods that a delegate class should implement.
Delegation is a simple and powerful pattern in which one object in a program acts on behalf of, or in coordination with, another object. The delegating object keeps a reference to the other object—the delegate—and at the appropriate time sends a message to it. The message informs the delegate of an event that the delegating object is about to handle or has just handled. The delegate may respond to the message by updating the appearance or state of itself or other objects in the application, and in some cases it can return a value that affects how an impending event is handled. The main value of delegation is that it allows you to easily customize the behavior of several objects in one central object. (source)
Delegates should conform to a protocol
.
id<ADelegatesProtocol> delegate
means, that the object delegate
needs to conform to the protocol ADelegatesProtocol
I find the easiest way to explain this would be through a common delegate in UIKit, like UIAlertViewDelegate
. Here's some sample code:
In your .h file, you would say that your class conforms to a delegate, like this:
@interface Foo : UIViewController <UIAlertViewDelegate> { // ...
This tells the compiler that the class Foo
implements some or all methods of a delegate.
When you instantiate a UIAlertView
, you specify what the delegate is for the object:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title"
message:@"Message"
delegate:self
cancelButtonTitle:@"Okay"
otherButtonTitles:@"Cancel", nil];
[alert show];
[alert release];
Note that we're saying self
is the delegate.
Now, you want to implement the necessary methods:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
NSLog(@"%d got clicked", buttonIndex);
}
For UIAlertView
, there's a property of type id<UIAlertViewDelegate>
, which ties this back to your original question.
I'm fairly new to Objective-C/iPhone development and one thing I found to be super useful is to look for an associated xxxxDelegate
in the "Overview" section of a class. You're bound to find useful (in other languages' parlance) events that get fired for common actions.
Hope this helps!
精彩评论