如何在NSWindow中显示sheet视图

问题描述 投票:0回答:1

如何实现下图中的视图。
System Preferences > Network 中单击

+
按钮时出现的视图 enter image description here


我有以下问题:

  1. 这个视图系统有具体的名字吗(比如popover),因为我在Mac的很多地方都见过它。
  2. 如何在IB中实现?
  3. 这可以在弹出窗口而不是 NSWindow 中完成吗?(或者只能在 NSWindow 之类的工具栏中完成)
macos cocoa appkit
1个回答
2
投票

在 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 文件中实现窗口要容易得多。例如,见下文,

NSWindow containing a items to be displayed in the sheet.

现在您需要在您的自定义 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 上的示例项目。

© www.soinside.com 2019 - 2024. All rights reserved.