如何显示从xib加载的工作表?(MACOSX)

Bru*_*sso 4 macos cocoa nswindow nswindowcontroller

我有一个只有NSPanel的xib文件,我试图将此面板显示为模态表(带beginSheet:modalForWindow:modalDelegate:didEndSelector:contextInfo:).该xib的文件所有者是一个控制器类"MyController",它具有NSPanel的IBOutlet.

我正在寻找的是:

...
MyController *controller = [[MyController alloc] init];

[NSApp beginSheet:controller.panel modalForWindow:[NSApp mainWindow] modalDelegate:controller didEndSelector:nil contextInfo:nil];
...
Run Code Online (Sandbox Code Playgroud)

问题:MyController必须继承自NSWindowController还是NSObject?我试过了NSWindowController,initWithWindowNibName:但出口NSPanel始终是零.

谢谢

Bru*_*sso 8

我解决了 您必须停用几乎所有用于工作表的窗口对象(在IB中)的属性.我将以下方法添加到我的控制器以显示工作表:

- (void)showInWindow:(NSWindow *)mainWindow {
    if (!panelSheet)
        [NSBundle loadNibNamed:@"XibName" owner:self];

    [NSApp beginSheet:panelSheet modalForWindow:mainWindow modalDelegate:nil didEndSelector:nil contextInfo:nil];
    [NSApp runModalForWindow:panelSheet];   //This call blocks the execution until [NSApp stopModal] is called
    [NSApp endSheet:panelSheet];
    [panelSheet orderOut:self];
}
Run Code Online (Sandbox Code Playgroud)

panelSheet 是工作表窗口的IBOutlet.

感谢Jon Hess和JWWalker的帮助