在NSWindow中添加向下滑动视图 [英] Adding slide-down view in NSWindow

查看:57
本文介绍了在NSWindow中添加向下滑动视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何实现下图中的视图.
System Preferences > Network中单击 + 按钮时出现的视图

How do I implement the view in following image.
The view which appears when + button is clicked in System Preferences > Network

我有以下问题:

  1. 此视图系统是否具有特定名称(如popover),因为我在Mac的许多地方都看到过它.
  2. 如何在IB中实现它?
  3. 是否可以在弹出窗口中而不是在NSWindow中完成此操作?(或者是否只能在NSWindow之类的工具栏上使用)

推荐答案

在可可粉中,这些被称为床单.看看工作表编程指南,但是,这已经过时了!

In Cocoa these are called sheets. Take a look at the sheet programming guide, however, this is terribly out of date!

您需要在要显示图纸的窗口上调用-beginSheet:completionHandler:.如果您有单窗口应用程序,则可以向AppDelegate询问窗口并像这样启动工作表,

You need to call -beginSheet:completionHandler: on the window you want to display the sheet. If you have single-window application you can ask the AppDelegate for the window and launch the sheet like so,

// 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;
        }
    }];

您会注意到,工作表完成后会返回一定的模态响应---我们很快将返回到这一点.

You will notice that when the sheet completes it will return a certain modal response --- we will return to this point in a shortly.

接下来,您需要实现要在工作表中显示的内容;这必须在NSWindow中完成.我发现使用NSWindowController并在单独的XIB文件中实现窗口要容易得多.例如,请参见下文

Next you need to implement the content that you want to display in the sheet; this must be done in an NSWindow. I find it much easier to use a NSWindowController and implement the window in a separate XIB file. For example, see below,

现在,您需要在自定义NSWindowController 中实现代码(如果您是老式的并且喜欢管理自己的NIB加载,则可以使用普通的NSWindow),它将发出正确的模态响应.在这里,我已将取消"和确定"按钮连接到以下操作方法,

Now you need to implement the code in your custom NSWindowController (or plain NSWindow if you are old-school and love to manage your own NIB loading) which will issue the correct modal response. Here I have hooked up the cancel and OK buttons to the following actions methods,

- (IBAction)cancelButtonAction:(id)sender {
    [[[self window] sheetParent] endSheet:self.window returnCode:NSModalResponseCancel];
}

- (IBAction)OKButtonAction:(id)sender {
    [[[self window] sheetParent] endSheet:self.window returnCode:NSModalResponseOK];
}

模型响应将发送到您的完成处理程序块.

The model response will get sent to your completion handler block.

在github上的示例项目.

这篇关于在NSWindow中添加向下滑动视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆