Clicking menu item shows a NSWindow, but only f开发者_JS百科or 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 NSWindowController
and that its window
outlet 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 initWithWindowNibName
loads the NIB file and the window is shown because of the Visible At Launch flag. But if you look at the window
property of your NSWindowController
instance you'll see that it is still nil. That's why the window isn't redisplayed when you call showWindow
again. 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:
[window makeKeyAndOrderFront:self]
Did you uncheck the "release when closed" checkbox in Interface Builder (not built into Xcode 4) for the window?
精彩评论