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

  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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值