2008-05-13 14:31:11 Nibless Window Example
No frills code to bring up a Cocoa window without using nib files:

(updated to work on Lion)

( objc )
  1  #import <AppKit/AppKit.h>
2 // clang -o nibless nibless.m -framework AppKit
4 @interface TestView : NSView <NSWindowDelegate> { }
5 -(void)drawRect:(NSRect)rect;
6 @end
8 @implementation TestView
10 -(void)drawRect:(NSRect)rect {
11 [[NSColor blueColor] set];
12 NSRectFill( [self bounds] );
13 }
15 -(void)windowWillClose:(NSNotification *)note {
16 [[NSApplication sharedApplication] terminate:self];
17 }
19 @end
21 int main(int argc, const char *argv[]) {
22 NSAutoreleasePool *pool = [NSAutoreleasePool new];
23 NSApplication *app = [NSApplication sharedApplication];
24 NSRect frame = NSMakeRect( 100., 100., 300., 300. );
26 NSWindow *window = [[NSWindow alloc]
27 initWithContentRect:frame
28 styleMask:NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask
29 backing:NSBackingStoreBuffered
30 defer:false];
32 [window setTitle:@"Testing"];
34 TestView *view = [[[TestView alloc] initWithFrame:frame] autorelease];
35 [window setContentView:view];
36 [window setDelegate:view];
37 [window makeKeyAndOrderFront:nil];
39 [app run];
41 [pool release];
42 return( EXIT_SUCCESS );
43 }
  • Ingemar Ragnemalm (Mon, October 12th, 2009, 5:18am UTC)
    Just what I needed! I have ported it to FPC; I hope you don't mind me publishing a derivate (with reference to you, of course). Excellent demo for my work on a nice cross-platform layer on Cocoa!

  • Kevin D. (Sat, December 11th, 2010, 8:58am UTC)
    Awesome. Exactly what I was after.

  • Narasimha Raja (Wed, September 14th, 2011, 1:31am UTC)
    I think Nibs are auto release objects. They are useful if your application has less than five or six screens(I am talking about iOS). Once the number of screens increase then your app will become slow or even crash. I hope nib less programming is better. It gives more control to the programmers.
    Suggestions and comments are welcome :)

    Narasimha Raja

  • Alex Gray (Sun, March 4th, 2012, 4:54pm UTC)
    I dont think this works in Lion anymor

    Untitled 54.m:36:25: warning: sending 'NSView *' to parameter of incompatible type 'id<NSWindowDelegate>'
    [window setDelegate:view];
    /System/Library/Frameworks/AppKit.framework/Headers/NSWindow.h:308:44: note: passing argument to parameter 'anObject' here
    - (void)setDelegate:(id <NSWindowDelegate>)anObject;
    1 warning generated.

  • Jeff (Sun, March 4th, 2012, 7:48pm UTC)
    This was old code from '08, but it just required a tiny tweak to get it to work with Lion, which requires an explicit use of the NSWindowDelegate method. Also, I corrected the view's type.

  • Cinolt (Fri, April 27th, 2012, 10:51pm UTC)
    I tried running this on Lion and a functional window does appear, but the window does not initially move to the front and it does not appear in the Alt+Tab list of windows.

    Any ideas on how I can make the window support these behaviors?

  • Bibhas (Sat, July 21st, 2012, 7:26pm UTC)
    This snippet is missing a:

    [NSApp activateIgnoringOtherApps:YES];

    You're welcome!

Leave a comment