The ConverterController class needs one action method, convert:. When the user clicks the Convert button, the convert: message is sent to the target object, an instance of the ConverterController class.
To add the convert: method to the ConverterController class, add the line in bold to the ConverterController class:
#import <Cocoa/Cocoa.h> |
@interface ConverterController : NSObject { |
IBOutlet NSTextField *amountField; |
IBOutlet NSTextField *dollarField; |
IBOutlet NSTextField *rateField; |
} |
- (IBAction)convert:(id)sender; |
@end |
Save the file. You’ll see how this method works in a later section.
Note: You might be asking yourself why the calculations aren’t done here in the controller. It would obviously be faster without the extra layer of communication. It is entirely possible to design an application this way, but the MVC design pattern was created for portability. In more complex applications, the underlying functionality may be operating system-dependant. If you make the controller have a method convert:, all the view needs to know is to call this method. From there, the various controllers written for the different operating systems can take care of calling the correct model functions. In this way, it’s similar to writing an API for this particular application.
Last updated: 2007-10-31