Stop modality when closing a window (Cocoa)

I am currently displaying a modal window with this code:

[[NSApplication sharedApplication] runModalForWindow:mainWindow]; 

However, when I close this window, the rest of the windows remain inactive. How to start the stopModal method when the window closes with red x?

Thanks,

Michael

+4
source share
4 answers

You can create a delegate for the window and answer it either on - (void) windowWillClose: (NSNotification *) notification or
- (void) windowShouldClose: (NSNotification *) notification :

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

See Mac Dev Center: NSWindowDelegate Protocol Link

+9
source

If you have a dialog that relates to a specific window, you probably shouldn't use a modal dialog other than a worksheet. Modal dialogs should be avoided if possible. If you use a worksheet, the problem you are facing will no longer be a problem.

 - (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]; } 
+2
source

Along with Randall's answer, you can associate a controller class with a delegate for the window defined in your .xib file.

You can handle

 [[NSApplication sharedApplication] stopModal]; 

in

  • -(void)performClose:(id)sender

  • -(void)windowWillClose:(NSNotification *)notification

methods.

0
source

Swift 4 (note that previous methods are deprecated):

 window.beginSheet(self.uiSettingsPanel, completionHandler: {response in NSLog("Finished sheet, response: \(response)") }) 

where self.uiSettingsPanel is an instance of an NSPanel subclass. Then in the NSPanel subclass for the sheet, close it with something like

 @IBAction func buttonOK(_ sender: NSButton) { self.sheetParent!.endSheet(self, returnCode: .OK) } 
0
source

Source: https://habr.com/ru/post/908087/


All Articles