I have an NSWindow set up in Interface Builder. I have set the class of File's Owner to my NSWindowController and linked the window property of the controller to my NSWindow.
My controller implements NSWindowDelegate.
Now, in my controller, I have added the following:
- (void)windowDidLoad
{
[super windowDidLoad];
[self.window setDelegate:self];
}
- (void)windowDidBecomeMain:(NSNotification *)notification
{
NSLog(@"Did become main.");
}
Still, -windowDidBecomeMain: isn’t called. Does anyone know why this is?
EDIT:
Trying to show a window from AppDelegate on launch. The main nib (declared in Info.plist) contains a menu item only which is linked to the AppDelegate. In the application delegate, I show an icon on the status bar and when this icon is clicked, I display the menu from the main nib.
In the application delegate, I also want to display a window which should have a window controller assigned to take care of the logic.
I believe that when this works, I will receive my window notifications.
Now, the following code doesn’t show the window and I can’t figure out why.
DemoWindowController *dwc = [[DemoWindowController alloc] initWithWindowNibName:@"DemoWindowController"];
[dwc showWindow:self];
Note that self is the application delegate.
I suspect your problem is due to the fact that your window controller is not actually the object that is the nibs file owner.
When you change the class in interface builder you are telling it what outlets and actions are available (which is why you are able to drag to the window outlet) but you are still responsible for passing in this object yourself.
In the case of a non-document based application, you will have a main method which calls
NSApplicationMain. What this does is basically look up and load the window nib that is specified in your info.plist file and pass the currentNSApplicationinstance to this nib as the files owner (so even though you changed the class type toNSWindowController, the object being passed in is actually of type NSApplication).The easiest way to fix your problem is to get rid of your window controller for now (as it isn’t actually doing anything yet).
You should implement the
-windowDidBecomeMain:method in your app delegate. Then Ctrl+drag from your window to your appDelegate to set it as the delegate of the window to get your notifications.Update
To answer your question regarding the
WindowControllerbeware of the following two issues:applicationDidFinishLaunching:method. This is released the moment you leave the method taking your window with it. Create an instance variable to hold onto the window controller instead.Your window should now display.