我有一个窗口(及其 Controller )应该阻止所有其他窗口。 因此,我想使用 [NSApp runModalForWindow..] 显示窗口。
问题是谁负责调用这个方法:
- 窗口的 Controller (也许我应该重写 showWindow 函数)。
- 创建 Controller 的对象。那么,谁应该负责调用 stopModal?
最佳答案
在我的应用程序中,我有一个实现 NSApplicationDelegate 协议(protocol)的类 - 如果您刚刚开始一个简单的项目,您会得到什么。该对象有一个带有控件等的 View ,其中一些控件可以启动对话框。因此,在我需要显示应用程序模式对话框作为对控件的响应的情况下,在该控件的处理程序中(在我的 NSApplicationDelegate 对象中)我调用 runModalForWindow。假设我有一个派生自 NSWindowController 的名为 MyDialog 的类,其中包含一个对象 MyDialog* theDialog。该调用如下所示:
NSResult result= [NSApp runModalForWindow:[theDialog window]];
显示的对象有责任调用 abortModal、stopModal 或 stopModalWithCode。因此,在 MyDialog 类中,我可能有一个“确定”按钮的处理程序和一个“取消”按钮的处理程序:
-(void) onOk:(id)sender
{
[NSApp stopModal]; // Returns NSRunStoppedResponse.
[[self window] performClose:self];
}
-(void) onCancel:(id)sender
{
[NSApp abortModal]; // Returns NSRunAbortedResponse.
[[self window] performClose:self];
}
关于objective-c - - 谁应该负责调用 runModalForWindow - Controller 或初始化 Controller 的人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6408188/