首先,在支持文件 -> main.m 中注释掉 NSApplicationMain。 NSApplicationMain() 加载 Info.plist 中提到的主笔尖,所以跳过它。相反,设置应用程序并委托并运行应用程序:
int main(int argc, const char * argv[])
{
//return NSApplicationMain(argc, argv);
@autoreleasepool {
NSApplication * application = [NSApplication sharedApplication];
MYAppDelegate* appDelegate = [[MYAppDelegate alloc] init];
[application setDelegate:appDelegate];
[application run];
}
return EXIT_SUCCESS;
}
然后,在应用代理的 applicationDidFinishLaunching: 函数中,调用类似于 createMainWindow: 的东西:
- (void)createMainWindow
{
self.wincon = [[MYCustomWindowController alloc] initWithWindowNibName:@"MainMenu"];
self.window = self.wincon.window; // window property in appdelegate created for single-view app
// Also had to connect About: to application's orderFrontStandardAboutPanel
}
MainMenu.xib 的 File's Owner 自定义类应该从应用程序切换到 MYCustomWindowController。
如果 MainMenu.xib 有一个像这个例子一样的窗口,它的“引用出口”需要连接到 File's Owner->window。
如果您从单个视图应用程序开始,请从 MainMenu.xib 中删除 App Delegate 对象 - 否则 xib 将创建您的应用程序委托的第二个实例。如果您引用 MYAppDelegate.managedObjectContext 之类的东西,这可能会很糟糕。如果需要绑定应用委托,可以通过delegate.managedObjectContext的键路径绑定到Application。
我为什么要这样做?因为有时我的应用程序会使用 GUI 启动,有时则不会。