【问题标题】:How show sheet loaded from xib? (MacOSx)如何显示从 xib 加载的工作表? (MacOSx)
【发布时间】:2010-11-05 15:12:21
【问题描述】:

我有一个 xib 文件,其中只有一个 NSPanel,我试图将此面板显示为模式表(使用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];
...

问题: MyController 必须从NSWindowControllerNSObject 继承吗?我试过NSWindowControllerinitWithWindowNibName:NSPanel 的出口总是为零。

谢谢

【问题讨论】:

  • NSWindowController 将在调用它的 'window' 访问器时延迟加载 XIB 文件。如果您将窗口控制器子类化,请尝试移除面板插座,并使用内置窗口插座。然后,当您调用访问者来展示工作表时,XIB 将被加载。 [NSBundle loadNibNamed:@"XibName" owner:self] 的顶级对象的内存处理很棘手。 NSWindowController 为您处理所有这些。我建议使用它。此外,您要关闭的检查器或窗口中的属性是“在启动时可见”。
  • 当你访问它的'window'属性时,NSWindowCntroller如何知道要加载什么xib?
  • 当你初始化一个 NSWindowController 时,你提供了 nib 的名字或路径。这就是它知道要加载什么笔尖的方式。

标签: cocoa macos nswindow nswindowcontroller


【解决方案1】:

我解决了。您必须停用您用于工作表的窗口对象(在 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];
}

panelSheet 是工作表窗口的 IBOutlet。

感谢 Jon Hess 和 JWWalker 的帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多