Frage

I'm creating my first NSDocument based application. I'm able to create new documents, both from scratch and by importing legacy files.

This app will allow multiple windows per document, so I am overriding makeWindowControllers. This method is currently very simple:

- (void) makeWindowControllers
{
    if (documentDatabase == nil) return;
    DataSheetWindowController * dswc = [[DataSheetWindowController alloc] initWithDatabase:documentDatabase];
    [self addWindowController: dswc];
}

The window appears as expected, however, the Save, Revert to Save, and other document enabled menus are disabled, as if the document was not in the responder chain.

As an experiment, I tried adding this method to my NSWindowController class:

- (void)saveDocument:(id)sender {
    [[self document] saveDocument:sender];
}

With this method in place, the Save menu item is enabled, and selecting it causes the document's save methods to be invoked.

From reading the documentation and other questions on Stack Overflow, it's clear that something is wrong -- I should NOT have to put this method in the NSWindowController class. I'm sure I've overlooked something silly, but for the life of me I cannot figure out what it is, or any other mention of this problem here or elsewhere on the web.

Some additional information that may be useful -- in IB, the window's owner and delegate are set to the NSWindowController. I created a method to display the responder chain (see How to inspect the responder chain?) and the document was not listed. Here is the output of the responder chain (however, since NSDocument is not a subclass of NSResponder, I'm not sure if it is supposed to be listed anyway).

RESPONDER CHAIN:
<NSClipView: 0x102344350>
<NSScrollView: 0x102344480>
<NSView: 0x102345040>
<NSWindow: 0x10234e090>

Since the saveDocument method I put into the NSWindowController class does work, that indicates to me that the window controller does know that it is associated with the document.

So -- any thoughts as to why the document is behaving as if it is not in the responder chain?

Updated info: After setting up a new document, the initWithType method includes this temporary line to make sure that the document status is edited:

[self updateChangeCount:NSChangeDone];

I have verified that isDocumentEdited returns true.

War es hilfreich?

Lösung

I'm going to suggest that the solution is the one pointed to here:

https://stackoverflow.com/a/9349636/341994

In the nib containing the window that the window controller will be loading, the File's Owner proxy needs to be of the window controller's class (select the File's Owner proxy and examine the Identity inspector to confirm / configure that), and its window outlet must be hooked to the window and the window's delegate outlet must be hooked to the File's Owner proxy (select the File's Owner proxy and examine the Connections inspector to confirm that).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top