在 Cocoa Objective-C 中创建模态对话框或窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1902618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a Modal Dialog or Window in Cocoa Objective-C?
提问by Pradeep Kumar
I need to create an modal dialog, which is to loaded from a nib file and should be displayed on a button click in the main window.
我需要创建一个模态对话框,它是从一个 nib 文件加载的,应该在主窗口中单击按钮时显示。
I can create a custom window in a nib file and load the custom dialog on button click, but it's not a modal dialog. I can switch back to the main window.
我可以在 nib 文件中创建自定义窗口并在单击按钮时加载自定义对话框,但它不是模式对话框。我可以切换回主窗口。
MyWindowControlleris the NSWindowControllersubclass.
I used the code below to display my window in response to the button event:
MyWindowController是NSWindowController子类。我使用下面的代码来显示我的窗口以响应按钮事件:
MyWindowController *pController = [[MyWindowController alloc]
initWithWindowNibName:@"nibfilename"];
[MyWindowController showWindow:self];
回答by Chuck
There are several ways to do this — and in fact two different kinds of modal dialog in OS X: application-modal and window-modal. Using the wrong one will annoy people. One is a sheet, which is attached to the window that it affects (save dialogs are an example of this). The other is application-modal, which blocks the whole application (open dialogs work this way, since they don't apply to any window that exists yet). Apple's sheet documentationshould help get you oriented.
有几种方法可以做到这一点——实际上 OS X 中有两种不同的模态对话框:应用程序模态和窗口模态。用错了会惹恼人。一个是工作表,它附加到它影响的窗口(保存对话框就是一个例子)。另一个是应用程序模式,它会阻止整个应用程序(打开的对话框以这种方式工作,因为它们不适用于任何尚不存在的窗口)。Apple 的工作表文档应该可以帮助您定位。
回答by Pradeep Kumar
Thank you...
谢谢...
(Example). Create a nib with name "About"
(例子)。创建一个名为“About”的笔尖
if(pAbtCtrl == nil)
pAbtCtrl = [[AboutWindowController alloc] initWithWindowNibName:@"About"];
pAbtWindow = [pAbtCtrl window];
[NSApp runModalForWindow: pAbtWindow];
[NSApp endSheet: pAbtWindow];
[pAbtWindow orderOut: self];

