objective-cmacoscocoainterface-buildercocoa-sheet

How to show sheet from separate NIB


How do I put a window in a separate NIB, give it its own NSWindowController, make it slide out as a sheet?

(Is this a typical thing to do with sheets?)

I am trying to show a custom sheet (a window that slides down from the title bar of the parent window) from my main window. What I'm trying to do is standard, I think, but I cannot find clear examples or explanations for how to do exactly what I want.

What I am trying to do:

  1. My app delegate owns the main window, which has a button to open a "settings" sheet.
  2. The "settings" sheet:
    • is in a separate NIB.
    • has file owner set to class SettingsWindowController, which is subclass of NSWindowsController
  3. When user clicks "settings", I am trying to use Apple's [sample code][1]
- (void)showCustomSheet: (NSWindow *)window
// User has asked to see the custom display. Display it.
{
    if (!settingsSheet) 
    //Check the settingsSheet instance variable to make sure the custom sheet does not already exist.
        [NSBundle loadNibNamed:@"SettingsSheet" owner: self];
        //BUT HOW DOES THIS MAKE settingsSheet NOT nil?

    [NSApp beginSheet: settingsSheet
            modalForWindow: window 
            modalDelegate: self 
            didEndSelector: @selector(didEndSheet:returnCode:contextInfo:) 
            contextInfo: nil]; 

    // Sheet is up here.

    // Return processing to the event loop
} 

Please excuse the following simplistic and numerous questions:

Thanks for your patience in reading all this!


Solution

    1. Create an instance of SettingsWindowController, use initWithWindowNibName:

    2. You don't want it visible at launch.

    3. See 1.

    4. Your instance variables will be available to SettingsWindowController