tags:

views:

375

answers:

1

I'm trying to write the programmatic equivalent of a nib file I've setup that contains two windows: a main window and sheet that appears after launch to prompt for credentials. Wiring these up in IB works fine, so long as one remembers to uncheck the "Visible at Launch" box on the sheet/window.

However I can't figure out what the API equivalent is of "Visible at launch". When I run my app using the programmatic version the sheet is detached and not the key view in the same way my app ran with the nib when "Visible at Launch" was checked. So my assumption, then, is that I'm missing the secret visible-at-launch sauce.

Does anyone know how to do this?

P.S. I know how to make this work in IB, I specifically want to figure out the code equivalent so please don't tell me to just use the nib. I know that.

+3  A: 

NSWindows are typically created hidden. So you shouldn't have to do anything; just don't show the window until you need it. Here's a simple example.

NSWindow *sheetWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 100, 100) styleMask:NSTitledWindowMask backing:NSBackingStoreBuffered defer:NO];
NSTextField *field = [[NSTextField alloc] initWithFrame: NSMakeRect(25, 25, 50, 50)];
[[sheetWindow contentView] addSubview:field];
[NSApp beginSheet:sheetWindow modalForWindow:[self window] modalDelegate:self didEndSelector:@selector(sheetDidEnd:) contextInfo:NULL];

The text field obtained keyboard focus when I ran the above.

In future, please provide code in cases like this—it's a lot easier to correct existing code than to write new code.

Nicholas Riley
Nicholas, sorry about the lack of code example. I have a simple test-case working so now I just need to figure out what the differences are between my working test-case and my actual code. Thanks for your help!
Alex Vollmer
Thank you gentlemen. I just ran into the same problem, and the difference appears to be the type of the window itself. I had mine set to `NSBorderlessWindowMask` and it did not work. When I changed it to `NSTitledWindowMask`, everything worked like a charm!
e.James