如何在NSWindow中显示工作表视图 [英] How to display sheet view in NSWindow

查看:52
本文介绍了如何在NSWindow中显示工作表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


我有以下问题:

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

更新:更新标题以提高可见度

解决方案

在可可粉中,这些被称为床单.看看

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


I have following questions:

  1. Does this view system has a specific name (like popover), because I have seen it in many places in Mac.
  2. How to implement it in IB ?
  3. Can this be done in a popover window instead of NSWindow ?(or is it only possible in NSWindow like toolbar)

Update: Updating the title for better visibility

解决方案

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

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.

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,

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.

Sample project on github.

这篇关于如何在NSWindow中显示工作表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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