Creating a Cocoa application without NIB files
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;}
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;}
Though this is a few years old question...
Here's minimal code snippet to bootstrap a Cocoa application in Swift.
import AppKitfinal class ExampleApplicationController: NSObject, NSApplicationDelegate { let window1 = NSWindow() func applicationDidFinishLaunching(aNotification: NSNotification) { window1.setFrame(CGRect(x: 0, y: 0, width: 800, height: 500), display: true) window1.makeKeyAndOrderFront(self) } func applicationWillTerminate(aNotification: NSNotification) { }}autoreleasepool { () -> () in let app1 = NSApplication.sharedApplication() let con1 = ExampleApplicationController() app1.delegate = con1 app1.run()}
Also, I am maintaining a bunch of programmatic examples for Cocoa including bootstrapping, window, menu creations.
See subprojects for desired language.