advertisement

Article:
  Working with Sheets in Cocoa
Subject:   A problem with the beginSheet exemple
Date:   2001-12-26 03:24:43
From:   juban
This is a very usefull article. Thank you for this help.


However, there seems that the way you set-up sheets in your exemple didn't work for me.


You wrote :


- (IBAction)openPrefsSheet:(id)sender
{
[NSApp beginSheet:prefsWindow
modalForWindow:mainWindow
modalDelegate:nil
didEndSelector:nil
contextInfo:nil];


[NSApp runModalForWindow:prefsWindow];


[NSApp endSheet:prefsWindow];
[prefsWindow orderOut:self];
}


- (IBAction)closePrefsSheet:(id)sender
{
[NSApp stopModal];
}


When I tried this with my application, the sheet didn't want to close.
I tried to put a [prefsWindow close] statement before the [NSApp stopModal].
It worked better, but then, two sheets were appearing successively.
Finaly I tried this :


- (IBAction)openPrefsSheet:(id)sender
{
[NSApp beginSheet:prefsWindow
modalForWindow:mainWindow
modalDelegate:nil
didEndSelector:nil
contextInfo:nil];
}


- (IBAction)closePrefsSheet:(id)sender
{

[NSApp endSheet:prefsWindow];
[prefsWindow close];
}


I think that the runModalForWindow is not necessary for sheets because sheets are alredy modal windows.
In all cases, sheets are going away only if there is a "close" statement (in my case).


An other point is, if you have some updates to make to the window for which you want to attach a sheet,
you have to send a "display" message to your window and call the NSBeginAlertSheet like this :


NSBeginAlertSheet(title, defaultButton, alternateButton, otherButton, mainWindow, self, nil, @selector(sheetDidEnd:returnCode:contextInfo:), nil, message);


If you don't do so, your sheet won't slide down nicely as expected.