在Xcode中使用Cocoa框架显示窗口

6
我在Xcode中构建一个框架,需要在调用函数时显示一个窗口。如何让我的框架显示我在Interface Builder中构建的窗口?非常感谢提供逐步说明!
谢谢, Chetan
3个回答

8

您可以这样调用:

    MyWindowController* controller = [[MyWindowController alloc] 
     initWithWindowNibName:@"Foo"];
    [controller showWindow:nil];

其中Foo是nib文件的名称,MyWindowController是NSWindowController的一个子类,你需要将其设置为nib文件的所有者。

在这种情况下,重要的是要子类化NSWindowController,因为它会自动在类所在的bundle中搜索nib文件。


1

使用NSWindowController作为窗口的文件所有者,然后只需调用[myWindowController showWindow:nil]即可。


我已经尝试这样做了,但当我从使用我所制作的框架的应用程序中调用显示窗口的函数时(即调用[myWindowController showWindow:nil]的函数),窗口不会显示。请问你能否明确一下我需要做什么才能使其正常工作?非常感谢! - Chetan

0
这可能是由于没有对创建的NSWindowController保持强引用所致。如果你不保留它,该对象将被删除。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接