3

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?

user3956212
  • 359
  • 2
  • 14
  • The window will always be managed by an `NSWindowController` class (or subclass) even in a document-based app. There is a one-to-many relationship between `NSDocument` and `NSWindowController`. – trojanfoe Aug 19 '14 at 13:50
  • Ok, but how can I change the `NSWindowController` object to be an instance of my subclass instead? – user3956212 Aug 19 '14 at 14:12
  • You'll have to check with a google search, but I believe you override `[NSDocument makeWindowControllers]`. – trojanfoe Aug 19 '14 at 14:18

1 Answers1

5
  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

Marek H
  • 5,173
  • 3
  • 31
  • 42
  • about that diagram... a NSDocument can have more NSWindowControllers, each having one NSWindow. Am i wrong? – Radu Simionescu Feb 12 '16 at 13:16
  • in my case, my custom MyDocument has tens of outlets. It seems to me that, if I want to have a custom NSWindowController, I must move all of these outlets to my custom window controller, and refactor all of the code? That would be just crazy – Radu Simionescu Feb 12 '16 at 13:21
  • This is because you have and existing app and you are starting your architecture late. You have to do a lot of refactoring. No other solution exists if you want to have custom nswindowcontroller – Marek H Feb 24 '16 at 16:21
  • thanks i got that. I think it is apples poor design. when you have a complex app, you simply cannot anticipate that you will need a custom nswindowcontroller, 2 years later in development. eventually we found a workaround for what we wanted to achieve. – Radu Simionescu Feb 24 '16 at 22:24