Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
147
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: UNLESS a document is being reverted in which case windowControllerDidLoadNib: has already been called
  29.     // if loadedStringValue == nil, this is being called from windowControllerDidLoadNib: when creating a new document rather than opening an existing one
  30.    
  31.     if (textField && loadedStringValue)
  32.     {
  33.         [textField setStringValue:loadedStringValue];
  34.     }
  35.    
  36.     [loadedStringValue release];
  37. }
  38.  
  39. - (void)windowControllerDidLoadNib:(NSWindowController *)aController
  40. {
  41.     [super windowControllerDidLoadNib:aController];
  42.    
  43.     [self updateDocumentInterfaceIfNeeded];
  44. }
  45.  
  46. // save the textfield's string value
  47. - (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
  48. {
  49.     if (outError)
  50.     {
  51.         *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
  52.     }
  53.    
  54.     NSMutableData *mutableData = [[NSMutableData alloc] init];
  55.     NSKeyedArchiver *keyedArchiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mutableData];
  56.    
  57.     [keyedArchiver encodeObject:[textField stringValue]
  58.                          forKey:@"ZGStringValue"];
  59.    
  60.     [keyedArchiver finishEncoding];
  61.     [keyedArchiver release];
  62.    
  63.     return [mutableData autorelease];
  64. }
  65.  
  66. // load the textfield's string value
  67. - (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
  68. {
  69.     if (outError)
  70.     {
  71.         *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
  72.     }
  73.    
  74.     NSKeyedUnarchiver *keyedUnarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
  75.    
  76.     loadedStringValue = [[keyedUnarchiver decodeObjectForKey:@"ZGStringValue"] copy];
  77.    
  78.     [self updateDocumentInterfaceIfNeeded];
  79.    
  80.     [keyedUnarchiver release];
  81.    
  82.     return YES;
  83. }
  84.  
  85. + (BOOL)autosavesInPlace
  86. {
  87.     return YES;
  88. }
  89.  
  90. // textFields sent action when user hits enter after entering text into it
  91. // this marks that the document has been edited
  92. - (IBAction)changeText:(id)sender
  93. {
  94.     [self updateChangeCount:NSChangeDone];
  95. }
  96.  
  97. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement