Problems creating NSWindow

I'm new to Cocoa, and I'm just experimenting with creating a window programmatically (without using Interface Builder).

I launch a new Cocoa application in Xcode, then I remove the window from the nib file in Interface Builder to replace it with my own.

In the main function, I add the code:

NSWindow* myWindow; myWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(10,100,400,300) styleMask:NSTitledWindowMask backing:NSBackingStoreBuffered defer:NO]; 

When I try to create and run the application, I get the following error message:

Error (1002) creating CGSWindow

Why is this happening??? What is CGSWindow, by the way?

Rainer

+4
source share
4 answers

You probably don't have a connection to the windows server yet. This is an NSApplication job, so try creating a collaborative application first .

If this does not help, I will just consider my usual application layout: subclass NSObject for the custom controller, create an instance of this application from the application delegate applicationWillFinishLaunching: and drop it into applicationWillTerminate: and your user controller init method will create a window. The application object will definitely work at this point (since main does nothing except calling NSApplicationMain ), which receives / creates the shared application and tells it to execute), so you definitely need to connect to the window server and therefore create a window.

+2
source

Move the code you wrote to the following method from the application delegation application implementation file -

 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { // Insert code here to initialize your application NSWindow* myWindow; myWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(10,100,400,300) styleMask:NSTitledWindowMask backing:NSBackingStoreBuffered defer:NO]; } 

and it should fully load your window.

It is worth noting that it is never safe and it is not good practice to create UI-related objects in your main function.

0
source

The only code that you probably want to use in your main () function in a Cocoa application is automatically created for you by Xcode (if you use it).

I suggest you add the code you want to add to your applicationDelegate -applicationDidFinishLaunching: method

 - (void) applicationDidFinishLaunching:(NSNotification *)aNotification { myWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(10,100,400,300) styleMask:NSTitledWindowMask backing:NSBackingStoreBuffered defer:NO];; } 
0
source

If you want to display a completely empty window from scratch, this is the code you need:

 //if you used a template this will be already in the file: int main(int argc, char *argv[]) { NSAutoreleasePool *pool = [[NSAutoReleasePool alloc] init]; int retval=UIApplicationMain(argc,argv,nil,@"SimpleWindowAppDelegate"); [pool release]; return retVal; } @implementation SimpleWindowAppDelegate : NSObject <UIApplicationDelegate> -(void) applicationDidFinishLaunching:(UIApplication *)application { UIWindow *window=[[UIWindow alloc] initWithFrame:[[UIDevice mainScreen] bounds]]; //You could create views and add them here: //UIView *myView=[[UIView alloc] initWithFrane:CGRectMake(0,0,50,50)]; //[window addSubView:myView]; //[myView release]; [window makeKeyAndVisible]; } @end 
-1
source

All Articles