Help with sheets

Discussion in 'Mac Programming' started by MacRumoron, Mar 26, 2007.

  1. MacRumoron macrumors 6502

    Sep 6, 2005
    I'm trying to open a sheet attached to a window, after choosing a file from an open panel which is attached to the same window.

    I tried opening the sheet from the didEndSelector: of the open panel, but the code gets executed immediately when the Open button is pushed, and not when the open panel is finished closing. This is a problem because before the open panel finishes closing, the new sheet opens then for some reason the main window closes, and I am just left with a sheet that is attached to nothing (it is just a square box floating and can't be moved).

    Any suggestions? Thanks in advance. :)
  2. garethlewis2 macrumors 6502

    Dec 6, 2006
    You should have two methods.

    One called something like
    - (IBAction)openSheet:(id)sender;

    Another called

    - (IBAction)closeSheet:(id)sender;

    And finally a callback method with the format


    Your open button should call openSheet and execute the command

    [NSApp beginSheet:nameofsheetwindow modalForWindow[NSApp mainwindow] modalDelegate:self didEndSelector:mad:selector(sheetDidEnd:blah:blahContext:) contextInfo:WHATEVER];

    The close button on the sheet should be attached to the method closeSheet and should contain at least the following

    [nameofsheetWindow orderOut:sender];

    // This returns event handling back to normal
    [NSApp endSheet:nameofsheetWindow returnCode:1];

    In the method starting with sheetDidEnd or whatever you want to call it, is where you retrieve the data from controls on the sheet.
  3. MacRumoron thread starter macrumors 6502

    Sep 6, 2005
    I don't think you understood what I wrote. I am trying to open a sheet after the user chooses a file from an NSOpenPanel sheet. The problem is the sheet opens while the NSOpenPanel sheet is still open which for some reason causes the main window to close.
  4. garethlewis2 macrumors 6502

    Dec 6, 2006

    I don't believe you can open a sheet from within a sheet. I have never seen it and I doubt it would work. Since the sheet when started with begin sheet, has no window, you can't attach another sheet to it, as there is no parent to begin with. You could try and attach this subsheet to the main parent window, but more than likely this would cause the first sheet to close or OS X to behave very oddly.
  5. caveman_uk Guest


    Feb 17, 2003
    Hitchin, Herts, UK
    Yes you can because I've done it.
  6. garethlewis2 macrumors 6502

    Dec 6, 2006
    I thought they deprecated this with Tiger and Leopard. Did you code that on Jaguar or Panther?
  7. garethlewis2 macrumors 6502

    Dec 6, 2006
    Yep, your right it does work, just knocked up a simple example that opens a sheet within another sheet. Doesn't do anything fancy just opens and closes the sheets within a sheet, but since you still call the method

    sheetDidEnd:returncode:contextinfo when you end the sheet I don't see how you couldn't get the data from it.
  8. Eraserhead macrumors G4


    Nov 3, 2005
    You may need to make a custom open panel to do what you want, rather than using

    - (void)beginSheetForDirectory:(NSString *)absoluteDirectoryPath file:(NSString *)filename types:(NSArray *)fileTypes modalForWindow:(NSWindow *)docWindow modalDelegate:(id)modalDelegate didEndSelector:(SEL)didEndSelector contextInfo:(void *)contextInfo

Share This Page