Eugene Gordin
Eugene Gordin

Reputation: 4107

windowControllerDidLoadNib is not getting called in OSX app

I've noticed a weird behavior of my OSX app (Document-based) and I'm feeling that I'm doing something wrong here.

When I run my app from Xcode it runs great and everything is working. If I quit the app by pressing command+Q it's all good.

When I quit the app by pressing stop button in Xcode the app stops like it would normally, but when I run it next time my windowControllerDidLoadNib is not being called until I click on my app's icon in the dock menu (the app is running though, I checked in activity monitor).

Document.m

- (NSString *)windowNibName
{
    return @"Document";
}

- (void)windowControllerDidLoadNib:(NSWindowController *)windowController
{
    [super windowControllerDidLoadNib:windowController];
}

I'm really confused with this behavior and I don't know what I'm doing wrong or if this is something how it should be.

Any kind of help is highly appreciated

Upvotes: 0

Views: 445

Answers (3)

Marek H
Marek H

Reputation: 5566

windowControllerDidLoadNib is called within NSDocument subclass only if

  1. windowController owner is Document of the XIB. Within XCode the XIB owner is defined NSDocument subclass, not NSWindowController subclass

  2. you override windowControllerDidLoadNib method

From implementation of NSWindowController:

...
[self windowDidLoad]
if ([self owner] != self) {
        if ([self respondsToSelector:@selector(windowControllerDidLoadNib:)]) {
                [[self owner] windowControllerDidLoadNib:self];
        }
}
...

If you need this behaviour simply add this code to your NSWindowController subclass

- (void)windowDidLoad {
    [super windowDidLoad];
    if ([self owner] == self && [self document]) {
        if ([[self document] respondsToSelector:@selector(windowControllerDidLoadNib:)]) {
            [[self document] windowControllerDidLoadNib:self];
        }
    }
}

Upvotes: 0

reviver
reviver

Reputation: 168

windowControllerDidLoadNib is called when it needs a window controller. When you click the dock icon, it will open a new document, or create a new one, this is a system style. App needs no window when in background, so I think there is nothing wrong.

Upvotes: 1

Hussain Shabbir
Hussain Shabbir

Reputation: 14995

Just you have to call below methof of documentcontroller for loading nib method

   NSDocumentController * docC= 
   [[yourDocumentCont alloc]init];
   [docC openDocument:self]

Upvotes: 0

Related Questions