user3956212
user3956212

Reputation: 359

Cocoa document based app, NSWindowController subclass as "main window"

I have a Cocoa document based app. I want the "main window" to be managed by my subclass of NSWindowController. I have created the subclass and laid out its interface in a .xib file with the same name. I ultimately want the same behaviour as if the NSDocument managed the window, but instead have it managed by an NSWindowController.

First of all, how do I do it? Second, are the anything special I have to think about when going with this approach, such as how to handle open and save?

Upvotes: 3

Views: 1417

Answers (1)

Marek H
Marek H

Reputation: 5566

  1. 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];
    }
    
  2. comment windowNibName & 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's window.
    //}
    
  3. Change Document.xib File Owner Class 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

Upvotes: 5

Related Questions