Nawyecky wrote:
1. in the "nstextfield class reference" documentation, the method name is clearly
textDidChange:. how am i supposed to know that it needs to be
controlTextDidChange (short of posting a topic here everytime i need to delegate an object)?
Well, in Objective-C (and every OO languages), you can send messages that are declared in the class of your object, but also every method that are declared in their superclasses. So that controlTetDidChange: is simply defined in the NSControl class which is the NSTextField super-class. That's called inheritance.
Nawyecky wrote:
2. i have several textfields in my window. how do i know which one i'm checking when i get the notification? suppose i delegate them all to my appcontroller object, how can appcontroller know which textfield has just been edited?
For that, you need to add outlets to your AppController class. An outlet is simply an instance variable which type is an object but that will be initialized by the nib file instead of being initialized explicitly by you.
You can define an outlet directly in Interface Builder for your class (don't forget to sync Xcode and IB by writing class files, if you already have written class files, the best way is to define the outlets in Xcode and read the files from IB), and then link your outlet to the object you want in IB.
Then, in your method implementation, you can see which object is sending the notification or the delegate method by comparing addresses :
AppController.h :
#import <Cocoa/Cocoa.h>
@interface AppController : NSObject
{
// Your outlets, IBOutlet is just part of the Interface Builder parser
IBOutlet NSTextField *numberField;
IBOutlet NSTextField *nameField;
// the rest of your ivars
}
// your methods
@end
AppController.m
#import "AppController.h"
@implementation
- (void)controlTextDidChange:(NSNotification *)aNotification
{
if([aNotification object] == numberField)
{
// your code if the number field send the notification
}
else if([aNotification object] == nameField)
{
// your code for the name field
}
}
@end
When you have a notification, you can generally retrieve the object who post the notification using the -[NSNotification object] message. As objects, in Objective-C, are always references to objects, which means that you manipulate addresses and not datas directly, you can simply compare with == operator the adresses to see which specific object you manipulate.
NB : You can't use a switch() {} structure to compare addresses, that's sad but that's like that sorry.
Also, when you use pure delegate methods... I don't consider Notifications to be pure delegate methods, those messages are sent to the delegate but also a notification is posted at the same time, whenever a notification is just a message sent to the delegate method.
So when you use pure delegate method you can retrieve the object that sent the message with the first argument of the method, that's how you distinguish notification from delegate methods.
For example :
- (BOOL)control:(NSControl *)control isValidObject:(id)object
Here, "control" argument is the object that sent the message, so you'll simply compare control with your outlets using == operator.
PS : Notifications are made to do actions depending on what happened in an object but not to modify the object that posted the notification, they're here just to tell that's something did happen. Wherever pure delegate methods are here to influence the object that sent the message, for example to prevent the object from doing something stupid (closing the app without saving for example), or even to confirm a good action.
Message was edited by: PsychoH13