Re: Programmatic Windows
Re: Programmatic Windows
- Subject: Re: Programmatic Windows
- From: Tom Waters <email@hidden>
- Date: Wed, 13 Jun 2001 14:11:16 -0700
On Wednesday, June 13, 2001, at 12:33 PM, email@hidden wrote:
Anyone have any thoughts about creating NSWindows in code? I keep
getting errors that say 'NSWindow doesn't respond to alloc.'
I'm writing a small app, and I don't want to load this window from a
nib. Any help would be greatly appreciated.
This may be completely unrelated to what you were asking, but here's
what I was thinking about. An app that has no NIB file at all.
Here's a no-nib app that creates a main menu and a window with a content
view. You can create more menus/items in setupMenu and put your own
view in makeContentView (there's a simple image view in there as a demo).
In PB, you go to Application Settings and remove the "Principal class"
and "Main nib file" settings. (you can also delete MainMenu.nib.
--------------------- RawApp.h -------------------------
#import <Cocoa/Cocoa.h>
@interface RawApp : NSApplication {}
@end
--------------------- RawApp.m -------------------------
#import "RawApp.h"
@implementation RawApp
- (void)setupMenu
{
id item;
[self setMainMenu: [[NSMenu allocWithZone:[NSMenu menuZone]]
initWithTitle:@"mainmenu"]];
item = [[NSMenuItem allocWithZone:[NSMenu menuZone]]
initWithTitle:@"RawApp" action:NULL keyEquivalent:@""];
[[self mainMenu] addItem:item];
[item setSubmenu: [[NSMenu allocWithZone:[NSMenu menuZone]]
initWithTitle:@"File"]];
[[item submenu] addItem:[[NSMenuItem allocWithZone:[NSMenu
menuZone]] initWithTitle:@"Quit" action:@selector(terminate:)
keyEquivalent:@"q"]];
}
- (NSView *)makeContentView
{
NSImageView *iv = [[[NSImageView alloc] init] autorelease];
[iv setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
[iv setImage: [NSImage imageNamed: @"NSApplicationIcon"]];
return iv;
}
- (id)init
{
self = [super init];
[self setupMenu];
_mainWindow = [[NSWindow alloc] initWithContentRect:
NSMakeRect(100,100,320,240)
styleMask: NSTitledWindowMask | NSClosableWindowMask |
NSMiniaturizableWindowMask | NSResizableWindowMask
backing: NSBackingStoreBuffered
defer: NO];
[_mainWindow setContentView: [self makeContentView]];
[_mainWindow makeKeyAndOrderFront: nil];
return self;
}
@end
------------------------------- here is main.m ------------------------
#import <Cocoa/Cocoa.h>
#import "RawApp.h"
int main(int argc, const char *argv[])
{
id pool = [[NSAutoreleasePool alloc] init];
[[RawApp sharedApplication] run];
[pool release];
return 0;
}