Advertisement
Guest User

Untitled

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