Advertisement
Guest User

Untitled

a guest
Jul 24th, 2011
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #import "MyDocument.h"
  3.  
  4. /*
  5.  -- MyDocument.h --
  6.  #import <Cocoa/Cocoa.h>
  7.  
  8.  @interface MyDocument : NSDocument
  9.  {
  10.     IBOutlet NSTextField *textField;
  11.     NSString *loadedStringValue;
  12.  }
  13.  
  14.  - (IBAction)changeText:(id)sender;
  15.  
  16.  @end
  17. */
  18.  
  19. @implementation MyDocument
  20.  
  21. - (NSString *)windowNibName
  22. {
  23.     return @"MyDocument";
  24. }
  25.  
  26. - (void)updateDocumentInterfaceIfNeeded
  27. {
  28.     // if textField outlet == nil, this is being called from readFromData: ofType: error:
  29.     // if textField outlet != nil, this is being called from windowControllerDidLoadNib: OR readFromData: ofType: error: when reverting a document where windowControllerDidLoadNib: has already been called.
  30.     // if loadedStringValue == nil, this is being called from windowControllerDidLoadNib: when creating a new document
  31.    
  32.     if (textField && loadedStringValue)
  33.     {
  34.         [textField setStringValue:loadedStringValue];
  35.        
  36.         [loadedStringValue release];
  37.         loadedStringValue = nil;
  38.     }
  39. }
  40.  
  41. - (void)windowControllerDidLoadNib:(NSWindowController *)aController
  42. {
  43.     [super windowControllerDidLoadNib:aController];
  44.    
  45.     [self updateDocumentInterfaceIfNeeded];
  46. }
  47.  
  48. // save the textfield's string value
  49. - (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
  50. {
  51.     if (outError)
  52.     {
  53.         *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
  54.     }
  55.    
  56.     NSMutableData *mutableData = [[NSMutableData alloc] init];
  57.     NSKeyedArchiver *keyedArchiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mutableData];
  58.    
  59.     [keyedArchiver encodeObject:[textField stringValue]
  60.                          forKey:@"ZGStringValue"];
  61.    
  62.     [keyedArchiver finishEncoding];
  63.     [keyedArchiver release];
  64.    
  65.     return [mutableData autorelease];
  66. }
  67.  
  68. // load the textfield's string value
  69. - (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
  70. {
  71.     if (outError)
  72.     {
  73.         *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
  74.     }
  75.     NSKeyedUnarchiver *keyedUnarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
  76.    
  77.     loadedStringValue = [[keyedUnarchiver decodeObjectForKey:@"ZGStringValue"] copy];
  78.    
  79.     [self updateDocumentInterfaceIfNeeded];
  80.    
  81.     [keyedUnarchiver release];
  82.    
  83.     return YES;
  84. }
  85.  
  86. + (BOOL)autosavesInPlace
  87. {
  88.     return YES;
  89. }
  90.  
  91. // textFields sent action when user hits enter after entering text into it
  92. // this marks that the document has been edited
  93. - (IBAction)changeText:(id)sender
  94. {
  95.     [self updateChangeCount:NSChangeDone];
  96. }
  97.  
  98. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement