Search code examples
macoscocoansviewcocoa-sheet

How to show a view in this way on Cocoa Desktop?


I'm doing an app and I was wondering how can you show a view like this:

dropdownwindow


Solution

  • That would be called a sheet. This guide should walk you through how to do them.

    You basically make a NSWindow that you would like to use as a sheet, then, when you want to show it, call:

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

    myCustomSheet is obviously your sheet and window is the window you want it to appear in. Set self as the delegate and implement didEndSheet:returnCode:contextInfo::

    - (void)didEndSheet:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
    {
        [sheet orderOut:self];
    }
    

    Hook up a "close" button on your sheet to an IBAction that closes the sheet.

    - (IBAction)closeMyCustomSheet: (id)sender
    {
        [NSApp endSheet:myCustomSheet];
    }
    

    These sheets don't even need to be folded! XD