views:

111

answers:

5

Hi,

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

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

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 build and run the application, I receive the following error message:

Error (1002) creating CGSWindow

Why does this happen??? What is a CGSWindow by the way?

Rainer

A: 

You probably don't have a connection to the window server yet. That's NSApplication's job, so try creating the shared application first.

If that doesn't help, I'd just go through with my usual application layout: Create an NSObject subclass for a custom controller, instantiate this from your application delegate's applicationWillFinishLaunching: and release it in applicationWillTerminate:, and have your custom controller's init method create the window. The application object will definitely be running by this point (as main does nothing but call NSApplicationMain, which gets/creates the shared application and tells it to run), so you should definitely have your connection to the window server and so be able to create the window.

Peter Hosey
A: 

Thanks Peter. Since I am new to Cocoa I don't know much about these things yet . As I have described above, I selected "Cocoa Application" to create a new project. Then I removed the window from the nib file in Interface Builder to replace it with a window I wanted to create programamatically. So I added the code shown above to create this new window.

Could you provide an example code to show me how to do the things you have suggested?

RainerB
A: 

Move the code you've written into the following method from your App Delegate 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 load your window perfectly.

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

Adam Ko
A: 

If you want to display a completely empty window from scratch this is all the code you'll 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
Leg10n
lowell
True, I got confused, sorry
Leg10n
A: 

The only code that you will probably ever want to have in your main() function in a Cocoa application is automatically created for you by XCode (if you are using it).

I suggest that you add the code that you want into your applicationDelegate's -applicationDidFinishLaunching: method

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