Here is something that I haven’t found on the web: an example of how to create a NSWindow programatically from Cocoa. After a little bit of reading, and some head scratching, this is what I got:

NSRect windowRect = NSMakeRect(10.0f, 10.0f, 800.0f, 600.0f);

NSWindow *window = [[NSWindow alloc] initWithContentRect:windowRect 
styleMask:( NSResizableWindowMask | NSClosableWindowMask | NSTitledWindowMask) 
backing:NSBackingStoreBuffered defer:NO];

[window makeKeyAndOrderFront:nil];
 

There was another website where I saw some sample code that helped, so here it is: 
http://www.mp3car.com/vbulletin/maccar/73352-some-cocoa-xcode-help.html

5 Comments

  1. Actually, you must use the C bitwise OR, not the logical OR:

    NSWindow *window = [[NSWindow alloc] initWithContentRect:windowRect styleMask:( NSResizableWindowMask | NSClosableWindowMask | NSTitledWindowMask) backing:NSBackingStoreBuffered defer:NO];

    It took me a while to figure out why my buttons weren’t showing up!

    Robert

    1. Thanks for pointing that out. My eyes must have glazed over that and/or my blog editor might have added an extra pipe.

      Looking over this code, I see this was written before using the “pre” code wrapper. I adjusted the code. Thanks for the comment!

Leave a Reply

Your email address will not be published. Required fields are marked *