I have a window (with its controller) that should block all other windows. For that reason i want to show the window using [NSApp runModalForWindow..].
The question is who is the one responsible for calling this method:
- the controller of the window (maybe i should override the showWi开发者_开发知识库ndow function).
- the object that created the controller. In that case, who should be responsible for calling stopModal?
In my application, I have a class that implements the NSApplicationDelegate protocol - what you would get if you just started a simple project. That object has a view with controls and whatnot, and some of those controls can launch dialogs. So, in the case where I need to show an application-modal dialog as a response to a control, in the handler for that control (in my NSApplicationDelegate object) I call runModalForWindow. Say I have a class derived from NSWindowController called MyDialog, with an object MyDialog* theDialog. The call would look like:
NSResult result= [NSApp runModalForWindow:[theDialog window]];
It is the responsibility of the displayed object to call abortModal, stopModal or stopModalWithCode. So, inside MyDialog class I might have a handler for an "OK" button and a handler for a "Cancel" button:
-(void) onOk:(id)sender
{
[NSApp stopModal]; // Returns NSRunStoppedResponse.
[[self window] performClose:self];
}
-(void) onCancel:(id)sender
{
[NSApp abortModal]; // Returns NSRunAbortedResponse.
[[self window] performClose:self];
}
精彩评论