Clicking menu item shows a NSWindow, but only for first time, why is so that, and how to fix it?
-(IBAction)menuClick:(id)sender
{
if (!formsView) {
formsView = [[NSWindowController alloc] initWithWindowNibName:@"Forms"];
}
[formsView showWindow:self];
}
That’s the same code I use and it works. The problem must be elsewhere.
The problem isn’t the code, it’s the setup in Interface Builder. (I’m still using Xcode 3.2 so in my world Interface builder is a separate application.) Make sure the class for File’s Owner in the xib is set to
NSWindowControllerand that itswindowoutlet is connected to the window.If you don’t do this and Visible At Launch is set for the window, you’ll get exactly the symptom you describe. This is because
initWithWindowNibNameloads the NIB file and the window is shown because of the Visible At Launch flag. But if you look at thewindowproperty of yourNSWindowControllerinstance you’ll see that it is still nil. That’s why the window isn’t redisplayed when you callshowWindowagain. It’s the connections in IB that associate the window with the controller.Here’s a project that has IB set up correctly.
Here’s the relevant settings in IB: