在可可中,这些被称为片。看看表编程指南 https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Sheets/Tasks/UsingCustomSheets.html然而,这已经过时了!
你需要打电话-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;
}
}];
您会注意到,当工作表完成时,它将返回特定的模式响应——我们很快就会回到这一点。
接下来您需要实现要在工作表中显示的内容;这必须在 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];
}
模型响应将发送到您的完成处理程序块。
github 上的示例项目。 https://github.com/danieljfarrell/CocoaSheets