在Cocoa Application中的applicationDidFinishLaunching中加载主窗口 [英] Loading main window at applicationDidFinishLaunching in Cocoa Application

查看:456
本文介绍了在Cocoa Application中的applicationDidFinishLaunching中加载主窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Cooca应用程序中,MainMenu.xib是在标准模板中为您设置的。这个笔尖也已经使用应用程序委派来设置。在info.plist中,键Main nib file bas ename设置要在启动时加载的nib文件。

In a Cooca Application the MainMenu.xib is setup for you in the standard template. This nib has been setup with the application delegate too. In the info.plist the key "Main nib file bas ename" sets the nib file to load at startup.

我希望应用程序在没有nib的情况下启动,我想在应用程序的主代理中的applicationDidFinishLaunching中加载MainMenu.xib。

I want the application to start if possible without a nib, I want to load the MainMenu.xib at applicationDidFinishLaunching in the application's main delegate.

是否可以?

推荐答案

首先,注释NSApplicationMain在支持文件 - > main.m. NSApplicationMain()加载在Info.plist中提到的主要的nib,所以跳过它。而是,设置应用程序并委派并运行应用程序:

First, comment out NSApplicationMain in supporting files -> main.m. NSApplicationMain() loads the main nib mentioned in your Info.plist, so skip it. Instead, setup the app and delegate and run the application:

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;
}



<

Then, in the app delegate's applicationDidFinishLaunching: function, call something similar to 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的文件所有者自定义类应该从应用程序切换到MYCustomWindowController。

MainMenu.xib's File's Owner custom class should be switched to MYCustomWindowController from the application.

如果MainMenu.xib有一个窗口在这个例子中,它的引用插座需要连接到文件的所有者 - >窗口。

If MainMenu.xib has a window like in this example, it's "referencing outlet" needs to be connected to File's Owner->window.

如果你开始使用单个视图应用程序,DELETE App Delegate对象MainMenu.xib - 否则xib将创建你的应用程序委托的第二个实例。这可能是可怕的,如果你引用像MYAppDelegate.managedObjectContext。如果你需要绑定到应用程序委托,你可以绑定到一个关键路径为delegate.managedObjectContext的应用程序。

If you started with a single view application, DELETE the App Delegate object from MainMenu.xib -- otherwise the xib will create a second instance of your app delegate. This can be terrible if you're referencing something like MYAppDelegate.managedObjectContext. If you need to bind to the application delegate, you can bind to the Application with a key path of delegate.managedObjectContext.

为什么这样做?因为有时我的应用程序使用GUI启动,有时它不会。

Why did I do this? Because sometimes my application launches with a GUI, and sometimes it doesn't.

这篇关于在Cocoa Application中的applicationDidFinishLaunching中加载主窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆