views:

271

answers:

2

Hello all,

I am currently displaying a modal window by using this code:

[[NSApplication sharedApplication] runModalForWindow:mainWindow];

However, when I close this window, the other windows are still inactive. How do I run the stopModal method when the window is closed using the "red x"?

Thank you,

Michael

+2  A: 

You can create a delegate for the window and have it respond to either the
-(void)windowWillClose:(NSNotification *)notification or the
- (void)windowShouldClose:(NSNotification *)notification methods like so:

- (void)windowWillClose:(NSNotification *)notification {
   [[NSApplication sharedApplication] stopModal];`
}

See Mac Dev Center: NSWindowDelegate Protocol Reference

Randall
A: 

If you have a dialog that applies to a specific window, then you probably shouldn't be using a modal dialog but a sheet. Modal dialogs should be avoided if possible. If you use a sheet then the problem that you're experiencing will no longer be an issue.

- (void)showSheet:(id)sender
{
    [NSApp beginSheet:yourModalWindow 
        modalForWindow:windowThatSheetIsAttachedTo
        modalDelegate:self
        didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:) 
           contextInfo:nil];
}

- (void)sheetDidEnd:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{
    [sheet orderOut:self];
    [NSApp endSheet:sheet];
}
Rob Keniger