Find 'MyDocument.nib' in the left margin and double-click it to open the window in Interface Builder.
- Double-click 'File's Owner' in the main control window.
- Click on the 'Outlets' tab, then 'Add', and add 'textView' as an 'outlet'.
- Now go back to the first tab in the main control window and 'ctrl-drag' from the File's Owner icon to the text view in the window. An inspector will appear and at the top it will say 'Connections'.
- Make sure 'textView' is selected, then click 'Connect' in the lower right hand corner. Your inspector should now look like the second image at right.
What you've done is create an outlet - a sort of reference - to the text view that the program will know about. You've called it 'textView'. We shall be using this name a few more times.
Save the Interface Builder project and exit.
Back in the left margin of Xcode/Project Builder, click on 'MyDocument.h'. This will open the file in the editor. See that your file looks like the following.
#import <Cocoa/Cocoa.h>
@interface MyDocument : NSDocument
{
id textString, textView;
}
@end
You've added two so-called pointers: one is 'textView' which you used in Interface Builder; the other, 'textString', will be used shortly.
Save the file and close it with ⇧⌘W.
Now click on 'MyDocument.m' to open it. Now we shall write some code - but as you shall see it's very little, and it's easy for you to copy in.
You have to adjust three 'methods'. They're given in alphabetic order here. Just make sure yours look exactly like the following. When you finish, save the file.
- (NSData *)dataRepresentationOfType:(NSString *)aType
{
return [[textView string]
dataUsingEncoding:NSMacOSRomanStringEncoding];
}
- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType
{
return ((textString =
[[NSString alloc] initWithData:data
encoding:NSMacOSRomanStringEncoding]) != 0);
}
- (void)windowControllerDidLoadNib:(NSWindowController *)aController
{
[super windowControllerDidLoadNib:aController];
if (textString) {
[textView setString:textString];
[textString release]; textString = 0;
}
}
|