我有一个 xib 文件,其中只有一个 NSPanel,我试图将此面板显示为模式表(使用 beginSheet:modalForWindow:modalDelegate:didEndSelector:contextInfo:
)。该 xib 文件的所有者是一个 Controller 类“MyController”,它具有 NSPanel 的 IBOutlet。
我正在寻找的是这样的:
...
MyController *controller = [[MyController alloc] init];
[NSApp beginSheet:controller.panel modalForWindow:[NSApp mainWindow] modalDelegate:controller didEndSelector:nil contextInfo:nil];
...
问题:
MyController 必须继承自 NSWindowController
或 NSObject
吗?我尝试了 NSWindowController
和 initWithWindowNibName:
但 NSPanel
的导出始终为零。
谢谢
最佳答案
我解决了。您必须停用用于工作表的窗口对象(在 IB 中)的几乎所有属性。我将以下方法添加到 Controller 中以显示工作表:
- (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];
}
panelSheet
是工作表窗口的 IBOutlet。
感谢 Jon Hess 和 JWWalker 的帮助
关于cocoa - 如何显示从 xib 加载的工作表? (MacOSx),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4107227/