Doing a Modal Dialog in Objective C

See this URL:
http://developer.apple.com/documentation/Cocoa/Conceptual/Sheets/Tasks/UsingAppModalDialogs.html
This is a better modal dialog than in the Hillegass book pages 306-311. The Hillegass book is great showing the wiring of such a sheet – but the code below is better for the actual showing of the dialog. The Hillegass book uses the selector (nifty pet trick – but overkill for most modal dialogs).


Use this code (I have added some comments below):
– (void)showCustomDialog: (NSWindow *)window
// User has asked to see the dialog. Display it.
{
if (!myCustomDialog)
[NSBundle loadNibNamed: @”MyCustomDialog” owner: self];
[NSApp beginSheet: myCustomDialog
modalForWindow: window
modalDelegate: nil
didEndSelector: nil
contextInfo: nil];
[NSApp runModalForWindow: myCustomDialog];
// Dialog is up here.
// This coee is executed after the dialog closes
// Pull final values for variables from of the modal Dialog here
// Note – any type of return code needs to be placed in a class-global variable
NSLog([_sheetPasswordTextField stringValue]);
fprintf(stderr,”Return value = %d\n”,sheetResult);
[NSApp endSheet: myCustomDialog];
[myCustomDialog orderOut: self];
}
Listing 2 Closing an application-modal dialog
– (IBAction)closeMyCustomDialog: (id)sender
{
// If you have more than one button, set a class-global integer
// to indicate which button was pressed to close the dialog (OK, Login, etc)
sheetResult = 0;
[NSApp stopModal];
}