Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import "MyDocument.h"
- /*
- -- MyDocument.h --
- #import <Cocoa/Cocoa.h>
- @interface MyDocument : NSDocument
- {
- IBOutlet NSTextField *textField;
- NSString *loadedStringValue;
- }
- - (IBAction)changeText:(id)sender;
- @end
- */
- @implementation MyDocument
- - (NSString *)windowNibName
- {
- return @"MyDocument";
- }
- - (void)updateDocumentInterfaceIfNeeded
- {
- // if textField outlet == nil, this is being called from readFromData: ofType: error: UNLESS a document is being reverted in which case windowControllerDidLoadNib: has already been called
- // if loadedStringValue == nil, this is being called from windowControllerDidLoadNib: when creating a new document rather than opening an existing one
- if (textField && loadedStringValue)
- {
- [textField setStringValue:loadedStringValue];
- }
- [loadedStringValue release];
- }
- - (void)windowControllerDidLoadNib:(NSWindowController *)aController
- {
- [super windowControllerDidLoadNib:aController];
- [self updateDocumentInterfaceIfNeeded];
- }
- // save the textfield's string value
- - (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
- {
- if (outError)
- {
- *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
- }
- NSMutableData *mutableData = [[NSMutableData alloc] init];
- NSKeyedArchiver *keyedArchiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mutableData];
- [keyedArchiver encodeObject:[textField stringValue]
- forKey:@"ZGStringValue"];
- [keyedArchiver finishEncoding];
- [keyedArchiver release];
- return [mutableData autorelease];
- }
- // load the textfield's string value
- - (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
- {
- if (outError)
- {
- *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
- }
- NSKeyedUnarchiver *keyedUnarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
- loadedStringValue = [[keyedUnarchiver decodeObjectForKey:@"ZGStringValue"] copy];
- [self updateDocumentInterfaceIfNeeded];
- [keyedUnarchiver release];
- return YES;
- }
- + (BOOL)autosavesInPlace
- {
- return YES;
- }
- // textFields sent action when user hits enter after entering text into it
- // this marks that the document has been edited
- - (IBAction)changeText:(id)sender
- {
- [self updateChangeCount:NSChangeDone];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement