Skip to content

Instantly share code, notes, and snippets.

@cellularmitosis
Last active September 19, 2022 06:52
Show Gist options
  • Save cellularmitosis/548eeb7e71ed39c6002b933a289d12cb to your computer and use it in GitHub Desktop.
Save cellularmitosis/548eeb7e71ed39c6002b933a289d12cb to your computer and use it in GitHub Desktop.
A first Cocoa app for OS X Tiger 10.4 using Xcode 3.1.4

Blog 2022/3/21

<- previous | index | next ->

A first Cocoa app for OS X Tiger 10.4 using Xcode 3.1.4

Getting my feet wet with Cocoa development for PowerPC Macs.

This app simply draws a blue window. Note that this does not require using Interface Builder.

Create a new Xcode project, then paste this into main.m:

#import <Cocoa/Cocoa.h>


@interface BlueView: NSView
@end

@implementation BlueView
- (void)drawRect:(NSRect)rect {
    [[NSColor blueColor] setFill];
    NSRectFill(rect);
    [super drawRect:rect];
}
@end


@interface AppListener: NSObject
- (void)applicationDidBecomeActive;
@end

@implementation AppListener

- (void)applicationDidBecomeActive {
    NSLog(@"did become active.");
    NSWindow* window = [[NSApplication sharedApplication] keyWindow];
    NSLog(@"window: %@", window);
    NSView* view = [[BlueView alloc] init];
    [window setContentView:view];
}

@end


int main(int argc, char *argv[])
{
    AppListener* listener = [[AppListener alloc] init];
    [[NSNotificationCenter defaultCenter]
        addObserver:listener
        selector:@selector(applicationDidBecomeActive)
        name:NSApplicationDidBecomeActiveNotification object:nil];
    return NSApplicationMain(argc,  (const char **) argv);
}

screenshot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment