I want to stop modal when the user clicks the red close button in NSWindowController.
In the NSWindowController, there are "OK" and "Cancel" buttons.
- (IBAction)okButtonClicked:(id)sender
{
[NSApp stopModalWithCode:NSOKButton];
[self.window close];
}
- (IBAction)cancelButtonClicked:(id)sender
{
[NSApp stopModalWithCode:NSCancelButton];
[self.window close];
}
And when I click the red close button, the window will close and the modal doesn't stop. I've found the windowWillClose: function.
- (void)windowWillClose:(NSNotification *)notification
{
if ([NSApp modalWindow] == self.window)
[NSApp stopModal];
}
However,
if ([NSApp runModalForWindow:myWindowController.window] != NSOKButton)
return;
Even if I click the OK button, windowWillClose: function is called and runModalForWindow: function always returns NSCancelButton.
I can add the member variable into myWindowController as the result of the modal.
But I think that there'll be another generic way to resolve this problem.
I want to take an easy way.