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

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

问题描述

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


我有以下问题:

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

更新:更新标题以获得更好的可见性

解决方案

在 Cocoa 中,这些被称为工作表.查看

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天全站免登陆