user3956212
user3956212

Reputation: 359

Multiple windows per document (Cocoa document based app)

Let's say I have created a new Cocoa application and use Document-based application when I create the project. It will have one window, the one from the NSDocument subclass. How can I make it so that two (or more) windows belong to each document?

I have created an NSWindowController subclass with a .xib file, where I have created the interface. How can I show this window? And how does communication between the NSWindowController subclass and the NSDocument subclass work?

(I use core data, so it is really an NSPersistentDocument subclass, but I don't think it matters for this particular question.)

Upvotes: 2

Views: 808

Answers (1)

Marek H
Marek H

Reputation: 5576

Within your NSDocument

//Lazy instantiation of window controller
- (AdditionalWindowController *)additionalWC {
    if (!_additionalWC) {
        _additionalWC = [[AdditionalWindowController alloc] initWithWindowNibName:@"AdditionalWindow"];
    }
    return _additionalWC;
}

- (IBAction)openAdditionalWindow:(id)sender {
  self.additionalWC.document = self;
  [self.additionalWC showWindow:self];
}

or

- (IBAction)openAdditionalWindow:(id)sender {
  //addWindowController ignores redundant invocations.
  [self addWindowController:self.additionalWC];
  [self.additionalWC showWindow:self];
}

Within your AdditionalWindowController you can always call

id document = [self document];
//do what ever you want e.g. somethingDidChanged | direct method call of your document

Upvotes: 2

Related Questions