I’m currently trying learning Cocoa and I am not sure if I understand that correctly… It’s about delegates and controllers.
At first: What’s the difference between the two? Sometimes I see code where a class is called AppController, sometimes – with more or less the same content – AppDelegate.
So, if I understand it correctly, a delegate is a simple object that receives messages when a certain event occurs. For example:
@interface WindowController : NSObject <NSWindowDelegate>
@end
@implementation WindowController
- (void)windowDidMiniaturize:(NSNotification *)notification {
NSLog(@"-windowDidMiniaturize");
}
@end
Now, I use this code to make it a delegate of my window:
@interface TryingAppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
}
@property (assign) IBOutlet NSWindow *window;
@property (retain) WindowController *winController;
@end
With the following implementation:
@implementation TryingAppDelegate
@synthesize window;
@synthesize winController;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSLog(@"-applicationDidFinishLaunching:");
self.winController = [[WindowController alloc] init];
[window setDelegate:winController];
[self.winController release];
}
@end
And now, whenever I minimize window, it will send a -windowDidMiniaturize: message to WindowController. Do I have that right?
If so, why don’t you just subclass NSWindow rather than bothering with an additional class you have to take care of?
Delegates are especially useful when you want one object to coordinate several others. For instance, you might create an
NSWindowControllersubclass and make it it’s window’s delegate. That same window might contain several other elements (likeNSTextFields) that you want to make the window controller a delegate of. This way you don’t need to subclass the window and several of its controls. You can keep all the code that conceptually belongs together in the same class. In addition, delegates usually belong to the controller level of the Model-View-Controller concept. By subclassingNSWindowyou would move controller type code to the view level.A class can adopt any number of protocols, so
<NSWindowDelegate, NSTextFieldDelegate>is perfectly valid. You can then set your object as the delegate of any number of windows and text fields. To find out what delegate messages a class likeNSTextFieldsupports check out the class reference.The-delegateand-setDelegate:methods will usually point you to the proper protocol. In our case this isNSTextFieldDelegate. For classes that have been added to older version of Apple’s frameworks there often an additional section on delegate methods (either alongside “Class Methods” and “Instance Methods” or as a subsection of “Tasks”). Note that declaring your class as conforming to a delegate protocol will not have them magically delivered to your object – you must explicitly set it as the delegate:AppControllerandAppDelegateare just different naming conventions for the same type of class.