【发布时间】:2014-12-18 07:57:20
【问题描述】:
如何实现下图中的视图。
在System Preferences > Network中单击+按钮时出现的视图
我有以下问题:
- 这个视图系统是否有一个特定的名称(如 popover),因为我在 Mac 的很多地方都见过它。
- 如何在 IB 中实现?
- 这可以在弹出窗口而不是 NSWindow 中完成吗?(或者只能在类似工具栏的 NSWindow 中实现)
更新: 更新标题以获得更好的可见性
【问题讨论】:
如何实现下图中的视图。
在System Preferences > Network中单击+按钮时出现的视图
我有以下问题:
更新: 更新标题以获得更好的可见性
【问题讨论】:
在 Cocoa 中,这些称为工作表。看看sheet programming guide,但是,这已经非常过时了!
您需要在要显示工作表的窗口上调用-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];
}
模型响应将被发送到您的完成处理程序块。
【讨论】: