|
|
20. Click on "WordDelegate.m" under the Classes category in ProjectBuilder. The skeletal source file will be displayed, now it's time to write those 13 lines of code, and you'll see the beauty and elegance of Rhapsody!
|
|
***** WordDelegte.h (made by Interface Builder) *****
#import <AppKit/AppKit.h>
@interface WordDelegate : NSObject
{
id theText;
}
- (void)newText:(id)sender;
- (void)openText:(id)sender;
- (void)saveText:(id)sender;
@end
***** WordDelegte.m *****
#import "WordDelegate.h"
@implementation WordDelegate
- (void)newText:(id)sender
{
// empty out the text with the empty NSString:
[theText setString:@""];
// Set the window's title to be untitled:
[[theText window]setTitle:@"Untitled"];
// bring the window up in case the user has closed it:
[[theText window] makeKeyAndOrderFront:self];
}
- (void)openText:(id)sender
{
// Get a new Open Panel
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
// Have it run modal and look for files of "rtf" or "rtfd" type:
if ([openPanel runModalForTypes:[NSArray arrayWithObjects:@"rtf",@"rtfd",NULL]]) {
// we have a valid file, ask theText to read it in
[theText readRTFDFromFile:[openPanel filename]];
// Update the window's name with the filename, but in a readable way:
[[theText window]setTitleWithRepresentedFilename:[openPanel filename]];
// bring the window up in case the user has closed it:
[[theText window] makeKeyAndOrderFront:self];
}
}
- (void)saveText:(id)sender
{
// Get a new Save Panel:
NSSavePanel *savePanel = [NSSavePanel savePanel];
// Set it to save "rtfd" files:
[savePanel setRequiredFileType:@"rtfd"];
// Run modal, which returns YES if a valid path is chosen:
if ([savePanel runModal]) {
// Ask the text to write itself to the chosen filename
// But don't make it back up before
// Set atomically:YES if you want "save backups", slower but more secure
[theText writeRTFDToFile:[savePanel filename] atomically:NO];
// Update the title bar of the window
[[theText window]setTitleWithRepresentedFilename:[savePanel filename]];
}
}
@end
|
|