Cocoa document-based application, subclass NSWindowController as "main window"

I have an application based on a Cocoa doc. I want the "main window" to be managed by my subclass NSWindowController

. I created a subclass and laid out its interface in a .xib file with the same name. Ultimately I want the same behavior as if it NSDocument

were controlling this window, but instead it was controlled NSWindowController

.

First of all, how do I go about doing this? Secondly, something special that I have to think about when going with this approach, like how to handle open source and save?

+3


source to share


1 answer


  • Override makeWindowControllers with your own windowController instance

    //Lazy instantiation of window controller
    - (WindowController *)controller {
      if (!_controller) {
          _controller = [[WindowController alloc] initWithWindowNibName:@"Document"];
      }
    
      return _controller;
    }
    
    - (void)makeWindowControllers {
      [self addWindowController:self.controller];
    }
    
          

  • comment windowNibName and windowControllerDidLoadNib: aController methods

    //- (NSString *)windowNibName
    //{
    //  // Override returning the nib file name of the document
    //  // 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.
    //  return @"Document";
    //}
    
    //- (void)windowControllerDidLoadNib:(NSWindowController *)aController
    //{
    //  [super windowControllerDidLoadNib:aController];
    //  // Add any code here that needs to be executed once the windowController has loaded the document window.
    //}
    
          

  • Change the owner class of Document.xib file from NSDocument to your WindowController

XIB renaming

From your WindowController, you can send a message (call method) to your document class.



Also make sure you understand this diagram:

enter image description here

+4


source







All Articles