View Swapping(Chapter 29 of Cocoa Programming for Mac OS X)

it2022-05-08  7

  1  #import  " MyDocument.h "   2  #import  " DepartmentViewController.h "   3  #import  " EmployeeViewController.h "   4    5  @implementation MyDocument   6    7  -  (id)init   8  {   9      [super init];  10      viewControllers  =  [[NSMutableArray alloc] init];  11        12      ManagingViewController  * vc  =  [[DepartmentViewController alloc] init];  13      [vc setManagedObjectContext:[self managedObjectContext]];  14      [viewControllers addObject:vc];  15      [vc release];  16        17      vc  =  [[EmployeeViewController alloc] init];  18      [vc setManagedObjectContext:[self managedObjectContext]];  19      [viewControllers addObject:vc];  20      [vc release];  21        22       return  self;  23  }  24   25  -  ( void )displayViewController:(ManagingViewController  * )vc  26  {  27      NSWindow  * =  [box window];  28      BOOL ended  =  [w makeFirstResponder:w];  29       if  ( ! ended)   30      {  31          NSBeep();  32           return ;  33      }  34        35      NSView  * =  [vc view];  36        37      NSSize currentSize  =  [[box contentView] frame].size;  38      NSSize newSize  =  [v frame].size;  39       float  deltaWidth  =  newSize.width  -  currentSize.width;  40       float  deltaHeight  =  newSize.height  -  currentSize.height;  41      NSRect windowFrame  =  [w frame];  42      windowFrame.size.height  +=  deltaHeight;  43      windowFrame.origin.y  -=  deltaHeight;  44      windowFrame.size.width  +=  deltaWidth;  45        46      [box setContentView:nil];  47      [w setFrame:windowFrame display:YES animate:YES];  48        49      [box setContentView:v];  50  }  51   52  -  (NSString  * )windowNibName  53  {  54       //  Override returning the nib file name of the document  55       //  If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.  56       return   @" MyDocument " ;  57  }  58   59  -  ( void )windowControllerDidLoadNib:(NSWindowController  * )aController  60  {  61      [super windowControllerDidLoadNib:aController];  62      NSMenu  * menu  =  [popUp menu];  63       int  i, itemCount;  64      itemCount  =  [viewControllers count];  65       for (i = 0 ; i < itemCount; i ++ )  66      {  67          NSViewController  * vc  =  [viewControllers objectAtIndex:i];  68          NSMenuItem  * mi  =  [[NSMenuItem alloc] initWithTitle:[vc title] action:@selector(changeViewController:) keyEquivalent: @"" ];  69          [mi setTag:i];  70          [menu addItem:mi];  71          [mi release];  72      }  73        74      [self displayViewController:[viewControllers objectAtIndex: 0 ]];  75      [popUp selectItemAtIndex: 0 ];  76  }  77   78  -  (IBAction)changeViewController:(id)sender  79  {  80       int  i  =  [sender tag];  81      ManagingViewController  * vc  =  [viewControllers objectAtIndex:i];  82      [self displayViewController:vc];  83  }  84   85  -  (NSData  * )dataOfType:(NSString  * )typeName error:(NSError  ** )outError {  86       /*  87       Insert code here to write your document to data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning nil.  88      You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.  89       */  90       if  (outError) {  91           * outError  =  [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];  92      }  93       return  nil;  94  }  95   96  -  (BOOL)readFromData:(NSData  * )data ofType:(NSString  * )typeName error:(NSError  ** )outError {  97       /*  98      Insert code here to read your document from the given data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning NO.  99      You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead. 100       */ 101       if  (outError) { 102           * outError  =  [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL]; 103      } 104       return  YES; 105  } 106  107  @end

转载于:https://www.cnblogs.com/zhtf2014/archive/2011/04/02/2003334.html


最新回复(0)