如何实现下图中的视图。
在 System Preferences > Network
中单击
+按钮时出现的视图
我有以下问题:
在 Cocoa 中,这些被称为“sheet”。看看sheet编程指南,然而,这已经过时了!
您需要在要显示工作表的窗口上调用
-beginSheet:completionHandler:
。如果您有单窗口应用程序,您可以向 AppDelegate 请求窗口并像这样启动工作表,
// This code should be in AppDelegate which implement the -window method
NSWindow *targetWindow = [self window]; // the window to which you want to attach the sheet
NSWindow *sheetWindow = self.sheetWindowController.window // the window you want to display at a sheet
// Now start-up the sheet
[targetWindow beginSheet:sheetWindow completionHandler:^(NSModalResponse returnCode) {
switch (returnCode) {
case NSModalResponseCancel:
NSLog(@"%@", @"NSModalResponseCancel");
break;
case NSModalResponseOK:
NSLog(@"%@", @"NSModalResponseOK");
break;
default:
break;
}
}];
您会注意到,当工作表完成时,它将返回特定的模式响应——我们很快就会回到这一点。
接下来你需要实现你想要在sheet中显示的内容;这必须在 NSWindow 中完成。我发现使用 NSWindowController 并在单独的 XIB 文件中实现窗口要容易得多。例如,见下文,
现在您需要在您的自定义 NSWindowController 中实现代码 (如果您是老派并且喜欢管理自己的 NIB 加载,则可以使用普通的 NSWindow ),这将发出正确的模式响应。在这里,我将取消和确定按钮连接到以下操作方法,
- (IBAction)cancelButtonAction:(id)sender {
[[[self window] sheetParent] endSheet:self.window returnCode:NSModalResponseCancel];
}
- (IBAction)OKButtonAction:(id)sender {
[[[self window] sheetParent] endSheet:self.window returnCode:NSModalResponseOK];
}
模型响应将发送到您的完成处理程序块。