Tim
Tim

Reputation: 5054

In NSWindowController subclass, [self document] returns null

I am using a custom subclass of NSDocument and a custom subclass of NSWindowController. The problem is that I cannot reference my custom document from my custom window controller.

In IB, in the TKDocument NIB I have File's Owner set to TKWindowController.

In my TKDocument subclass I have:

- (void) makeWindowControllers {
     TKWindowController *controller = [[TKWindowController alloc] init];
     [self addWindowController:controller];
}

Then in my TKWindowController subclass I overrode setDocument to make sure it was being called:

- (void) setDocument(NSDocument *) document {
     NSLog(@"setDocument:%@", document);
     [super setDocument:document];
}

and then (again in TKWindowController) my action which references the document itself:

- (IBAction) plotClicked:(id) sender {
     TKDocument *doc = [self document];
     NSLog(@"plotClicked %@", doc);
}

The NSLog in setDocument outputs the string returned by my [TKDocument description] override as I'd expect; I only put it there to see if it was being called. However, doc in plotClicked is null.

What might I have done wrong?

EDIT: I believe the problem is to do with NIBs. My Document has its own NIB with File's Owner set to the custom controller as mentioned above. The plotClicked action is fired from a menu item in MainMenu.xib. I believe it's hitting a new instance of the controller which isn't associated with the current, active document.

So, how do I link the two? My question is really this: How do I obtain a handle to the current active document (or its windowcontroller) from MainMenu.xib?

Thanks

Upvotes: 0

Views: 1145

Answers (2)

Peter Hosey
Peter Hosey

Reputation: 96393

My Document has its own NIB with File's Owner set to the custom controller as mentioned above.

The File's Owner of a document nib should be the document. Consider that suspect #1.

The plotClicked action is fired from a menu item in MainMenu.xib. I believe it's hitting a new instance of the controller which isn't associated with the current, active document.

Did you put a window controller inside your main menu nib? If not, then that isn't the problem, since you must have wired up your plotClicked: menu item to the First Responder, and the window controller and its document will be in the responder chain.

If you did, then there's the solution: delete the window controller from the MainMenu nib and hook up your menu item to the First Responder, so that the action message goes down the responder chain, which will enable it to hit the document or window controller.

How do I obtain a handle to …?

The only Handles on the Mac come from Carbon; those Handles do not exist in Cocoa.

Upvotes: 1

Joshua Nozzi
Joshua Nozzi

Reputation: 61248

init is not a designated initializer of NSWindowController. You want one of these: – initWithWindow:, – initWithWindowNibName:, – initWithWindowNibName:owner:, or – initWithWindowNibPath:owner:.

Also, from the docs:

In your class’s initialization method, be sure to invoke on super either one of the initWithWindowNibName:... initializers or the initWithWindow: initializer. Which one depends on whether the window object originates in a nib file or is programmatically created.

Upvotes: 0

Related Questions