The "Visible At Launch" Gotcha

Saturday, 2009-01-03; 04:43:03

When I use sheets in a Cocoa project, I often put the sheet in its own nib file. I create a subclass of NSWindowController that handles sheet dismissal, and includes the sheet callback method as well as methods that respond to button clicks on the sheet. Then when I need that sheet, I just allocate and initialize a new controller, which calls NSWindowController’s -initWithWindowNibName: method to load a copy of the sheet from the nib.

Typically a sheet is only associated with one kind of parent window (like a document editor window), meaning it usually makes sense to simply include the sheet inside that parent window’s nib. But putting the sheet in a nib by itself is useful if you have a lot of different kinds of windows, but you want to be able to attach an identical sheet to any one of them. This is what I do with kill sheets in Memory Usage Getter, since they can attach to both the Tab View and List View windows, as well as to individual process history windows.

I was using the same technique when adding some sheets for TidyWeblogger, except I was encountering a weird problem: the sheets weren’t attaching to windows like they were supposed to do. I was passing a valid reference to the sheet and to the parent window when using NSApp’s beginSheet:modalForWindow:… method, too.

Instead, the sheets just appeared in midair, attached to no window in particular, and as such, they couldn’t be moved around either. Furthermore, when I clicked a button to dismiss the sheet, they didn’t go away immediately. They only disappeared from the screen when I started interacting with a different window.

I posted about this problem on Twitter, and Fraser Speirs pointed out the problem:

In the nib I created for my sheet, the “Visible At Launch” box for the sheet window itself was checked in the Behavior section of the Attributes inspector tab. As soon as the nib was loaded, the window would appear, instead of waiting for me to tell the sheet to attach to a parent window.

The fix is simple. For any sheets, make sure the “Visible At Launch” box is not checked. After saving the nib and recompiling, everything worked perfectly.

Technological Supernova   Software Development   Older   Newer   Post a Comment