Create NSWindow programatically in Cocoa
I hope this helps.
In file main.m:
int main(int argc, char *argv[])
{
WindowController *windowController = [ [ WindowController alloc ] init ];
return NSApplicationMain(argc, (const char **) argv);
}
In file WindowController.mm:
@implementation WindowController
- (id) init
{
self = [ super init ];
if( !self )
return nil;
[ NSBundle loadNibNamed: @"MainMenu" owner: self ];
return self;
}
- (void) awakeFromNib
{
NSRect windowRect = NSMakeRect( 200.0, 200.0, 400.0, 300.0 );
NSWindow *window = [ [ NSWindow alloc ] initWithContentRect:windowRect styleMask
NSResizableWindowMask | NSClosableWindowMask | NSTitledWindowMask) backing:NSBackingStoreBuffered defer:NO];
NSButton *button = [ [ NSButton alloc ] initWithFrame: NSMakeRect( 300.0, 20.0, 80.0, 50.0 ) ];
[ button setBezelStyle:NSRoundedBezelStyle];
[ button setTitle: @"Click" ];
[ [ window contentView ] addSubview: button ];
[window makeKeyAndOrderFront:nil];
}
- (void)dealloc
{
[ super dealloc ];
}
@end