Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatic equivalent of "Visible at Launch" for NSWindows

Tags:

cocoa

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.

like image 438
Alex Vollmer Avatar asked May 06 '09 03:05

Alex Vollmer


1 Answers

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.

like image 103
Nicholas Riley Avatar answered Nov 16 '22 06:11

Nicholas Riley