我有一个 Objective C 应用程序,它首先加载使用 Interface Builder 创建的窗口:
//in main()
[NSApplication sharedApplication];
[NSBundle loadNibNamed:@"MainMenu" owner:NSApp];
[NSApp run];
在 MainMenu.xib 我有一个带有按钮的窗口。我想在按下该按钮时以编程方式创建第二个窗口。
  //in MainMenu.xib Controller.h
  @class SecondWindowController;
  @interface Controller: NSWindowController {
     @private
     SecondWindowController *sw;
  }
  - (IBAction)onButtonPress:(id)object;
  @end
//in MainMenu.xib Controller.m
#import "SecondWindowController.h"
@implementation Controller
- (IBAction)onButtonPress:(id)object {
  sw = [[SecondWindowController alloc] initWithWindowNibName:@"SecondWindow"];
  [sw showWindow:self];
  [[self window] orderOut:self];
}
@end
SecondWindowController 继承自 NSWindowController 的地方。在 SecondWindowController.h 我有:
- (id)initWithWindow:(NSWindow*)window {
  self = [super initWithWindow:window];
  if (self) {
    NSRect window_rect = { {custom_height1, custom_width1}, 
                           {custom_height2,    custom_width2} };
    NSWindow* secondWindow = [[NSWindow alloc] 
                         initWithContentRect:window_rect
                         styleMask: ...
                         backing: NSBackingStoreBuffered
                         defer:NO];                         
  }
  return self;
}
而在 SecondWindow.xib 我什么都没有。当第一个窗口的按钮被按下时,第一个窗口消失并且应用程序关闭。我不想对第二个窗口使用 Interface builder 的原因是我想以编程方式初始化它。这是可能的吗?如果可以,实现这一目标的正确方法是什么?