0

I want to create 3 windows like this:

NSWindow *win1 = [[NSWindow alloc] initWithContentRect:rect1
                                             styleMask:uiStyle
                                               backing:backingStoreStyle
                                                 defer:NO];

and make one of them the main window, then get the main window, but I always get nil.

[win1 makeKeyAndOrderFront:win1];
[win2 makeKeyAndOrderFront:win2];
[win3 makeKeyAndOrderFront:win3];
[win2 makeMainWindow];

And all of these are nil:

NSLog(@"%@", [app mainWindow]);
NSLog(@"%@", [win1 isMainWindow]);
NSLog(@"%@", [win2 isMainWindow]);
NSLog(@"%@", [win3 isMainWindow]);
Mike Abdullah
  • 14,933
  • 2
  • 50
  • 75
Frank
  • 11
  • 2
  • 1
    Downvoter; please explain why you downvoted. This looks like a reasonable question to me. – trojanfoe Feb 01 '13 at 13:18
  • You shouldn't be logging the result of `[NSWindow isMainWindow]` using the format `%@` as it's a `BOOL`, not an object. Use format `%d` or `%u` instead. – trojanfoe Feb 01 '13 at 13:21
  • I just log the result information here, yes %d or %u is better, but I also get the 0, so I ask it here. – Frank Feb 02 '13 at 08:25

1 Answers1

0

I tried your code. It does return an instantiated NSWindow object. I didn't get nil at all. Can you please post more context to code snippet?

Secondly, it is far easier and manageable to create window in IB. Thirdly, check - (BOOL)canBecomeMainWindow before passing it the message makeMainWindow.

If you want to create custom windows programaticaly, I would suggest create a Window in MainMenu.xib. Get it's IBOutlet, for example window. Then create your windows win1, win2 etc and add then as child windows to your window using addChildWindow.

Andrew-Dufresne
  • 5,464
  • 7
  • 46
  • 68
  • @ Andrew-Dufresne Hi, how you get the object? is it win2? Here I want to code these in command-line, and test the mainwindow. Thanks for comments. – Frank Feb 02 '13 at 08:32
  • @Frank See this [SO questions](http://stackoverflow.com/q/656129/177116). It has the code you need to create NSWindow programmatically. – Andrew-Dufresne Feb 02 '13 at 11:43