Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Cocoa application without NIB files

Tags:

Yes, I know this goes against the whole MVC principle!

However, I'm just trying to whip up a pretty trivial application - and I've pretty much implemented it. However, I have a problem...

I create an empty project, copy all the frameworks over and set the build settings - and I get errors about the executable, or lack of executable. The build settings all appear fine, but it tells me there is no executable - it will build + run fine. However it doesn't run. There is no error either - it just appears to run very fast and cleanly! Unless I try and run GDB which politely tells me I need to give it a file first..

Running…  
No executable file specified.  
Use the "file" or "exec-file" command.

So I created a Cocoa application, removed all the stuff I didn't need (that is, the MainMenu.xib file..), and now I can compile my code perfectly. However it dies complaining that it's

"Unable to load nib file: MainMenu, exiting"

I have gone through the Project Symbols and see that the code actually relies upon the NIB file heavily, even if you don't touch it code-wise. (MVC again I guess..)

Is there a simple way to compile just what you code, no added NIB files, just the code you write and the frameworks you add? I assume it would be a blank project, but my experience tells me otherwise?!

like image 698
Moddy Avatar asked Jun 08 '10 12:06

Moddy


People also ask

What is NIB file type?

A nib file is a special type of resource file that you use to store the user interfaces of iOS and Mac apps. A nib file is an Interface Builder document.

What is NSApplicationMain?

The NSApplicationMain function sends shared to the principal class to obtain the global app instance ( NSApp )—which in this case will be an instance of your custom subclass of NSApplication . Many AppKit classes rely on the NSApplication class and may not work properly until this class is fully initialized.


2 Answers

This is the method I use in my applications. Sorry for the formatting, I hope you can make it out. I don’t know how to turn off the auto-formatting here.

Of course there will be no functioning main menu out of this example, that’s far too much code for me to write on a post like this :P - Sorry, out do some research on that ;)

This should get you started:

AppDelegate.h

@interface MyApplicationDelegate : NSObject <NSApplicationDelegate, NSWindowDelegate> {
    NSWindow * window;
}
@end

AppDelegate.m

@implementation MyApplicationDelegate : NSObject
- (id)init {
    if (self = [super init]) {
        // allocate and initialize window and stuff here ..
    }
    return self;
}

- (void)applicationWillFinishLaunching:(NSNotification *)notification {
    [window makeKeyAndOrderFront:self];
}

- (void)dealloc {
    [window release];
    [super dealloc];
}

@end

main.m

#import "AppDelegate.h"

int main(int argc, char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSApplication * application = [NSApplication sharedApplication];

    MyApplicationDelegate * appDelegate = [[[[MyApplicationDelegate]alloc] init] autorelease];

    [application setDelegate:appDelegate];
    [application run];

    [pool drain];

    return EXIT_SUCCESS;
}
like image 103
Casper B. Hansen Avatar answered Oct 09 '22 09:10

Casper B. Hansen


int main() {
    [NSAutoreleasePool new];
    [NSApplication sharedApplication];
    [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
    id menubar = [[NSMenu new] autorelease];
    id appMenuItem = [[NSMenuItem new] autorelease];
    [menubar addItem:appMenuItem];
    [NSApp setMainMenu:menubar];
    id appMenu = [[NSMenu new] autorelease];
    id appName = [[NSProcessInfo processInfo] processName];
    id quitTitle = [@"Quit " stringByAppendingString:appName];
    id quitMenuItem = [[[NSMenuItem alloc] initWithTitle:quitTitle
    action:@selector(terminate:) keyEquivalent:@"q"] autorelease];
    [appMenu addItem:quitMenuItem];
    [appMenuItem setSubmenu:appMenu];
    id window = [[[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 200, 200)
    styleMask:NSTitledWindowMask backing:NSBackingStoreBuffered defer:NO]
    autorelease];
    [window cascadeTopLeftFromPoint:NSMakePoint(20,20)];
    [window setTitle:appName];
    [window makeKeyAndOrderFront:nil];
    [NSApp activateIgnoringOtherApps:YES];
    [NSApp run];
    return 0;
}
like image 25
Bosko Popovic Avatar answered Oct 09 '22 08:10

Bosko Popovic