3

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.

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.

Is it possible?

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
the Reverend
  • 12,305
  • 10
  • 66
  • 121
  • 1
    The application's delegate is *inside* the NIB, so what you're describing isn't possible. What are you trying to accomplish? Perhaps there's another way. – Francis McGrew Jan 03 '12 at 06:13

2 Answers2

4

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's File's Owner custom class should be switched to MYCustomWindowController from the application.

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

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.

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

stevesliva
  • 5,351
  • 1
  • 16
  • 39
1

This is possible, but seldom worth the trouble IMO. If you have a bundle already there is little cost in including a small nib file (menu only; no window). If you want to load the rest of your UI from a separate nib file after launch, that's fine. But I recommend allowing MainMenu.nib to load and provide the main menu. (You're not clear on what problem you're trying to solve with your approach.)

That said, Lap Cat wrote a series of articles on this called "Working without a nib" that's worth reading. You'll want the last article The Empire Strikes Back where he includes the link to his nibless project. His technique still works in 10.7.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
Rob Napier
  • 286,113
  • 34
  • 456
  • 610