问题
I've got a subclass of an NSWindowController that I'm using to load a window from a nib and show it on the screen. Below is the code that is called when I want to show the window. On 10.6 when showCustomWindow is called the window is displayed, but on 10.5 this method has to be called twice to get the window to display.
-(IBAction)showCustomWindow:(id)sender
{
if(!windowController){
windowController = [[MyWindowController alloc] initWithWindowNibName:@"MyWindow"];
}
[windowController showWindow:self];
}
The window has "Visible at Launch" checked and unchecking it didn't seem to make a difference.
Edit: I realized that the problem I was having was not related to my NSWindowController or showWindow. I had that set up correctly. I did however find out that not all classes implement awakeFromNib. In one of my NSView subclasses (that was in the nib I was trying to load), i was calling [super awakeFromNib] which was giving me a "does not respond to selector" (but only on 10.5 which is strange). So, I could have just taken out [super awakeFromNib] but I opted for the hopefully more robust:
if([NSView instancesRespondToSelector:@selector(awakeFromNib)]) {
[super awakeFromNib];
}
That allowed my nib to load normally and showWindow to work properly.
回答1:
Visible at Launch should be unchecked if you want to use -showWindow:
to control the timing of that window's visibility.
Everything else seems right from what you've shown us so this is just a guess but did you forget to connect the window
outlet on your File's Owner object to the window in your nib?
回答2:
Are you calling -showWindow before the window has finished loading from its nib? You might want set a breakpoint in [MyWindowController awakeFromNib] to find out.
回答3:
Edit: OK Sorry I misunderstood your question and see that you need to call showWindow twice. I don't have an answer for that.
The behaviour you're seeing is correct since the method initWithWindowNibName:
won't actually load the nib. Nib loading happens lazily .. so after you invoke the showWindow
method, or some other method such as window
that requires the nib to load.
来源:https://stackoverflow.com/questions/1529248/why-do-i-have-to-call-showwindow-on-my-nswindowcontroller-twice-on-10-5