在 Xcode 中使用 Cocoa Framework 显示窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1956649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display Window with Cocoa Framework in Xcode
提问by Chetan
I'm building a framework in Xcode, and I need to display a window when a function is called. How do I get my framework to display a window that I build in Interface Builder? Step by step instructions would be greatly appreciated!
我正在Xcode中构建一个框架,我需要在调用函数时显示一个窗口。如何让我的框架显示我在 Interface Builder 中构建的窗口?分步说明将不胜感激!
Thanks, Chetan
谢谢,切坦
回答by Ken Aspeslagh
You'd call it like this:
你会这样称呼它:
MyWindowController* controller = [[MyWindowController alloc]
initWithWindowNibName:@"Foo"];
[controller showWindow:nil];
Where Foo is the name of the nib file, and MyWindowController
is a subclass of NSWindowController that you set to be the owner of the nib file.
其中 Foo 是 nib 文件的名称,并且MyWindowController
是您设置为 nib 文件所有者的 NSWindowController 的子类。
In this case, it's important to subclass NSWindowController because it will automatically search for the nib file within the bundle that the class lives in.
在这种情况下,对 NSWindowController 进行子类化很重要,因为它会自动在类所在的包中搜索 nib 文件。
回答by Dave DeLong
Use an NSWindowController
as the window's File's Owner, and then just call [myWindowController showWindow:nil]
.
使用 anNSWindowController
作为窗口的文件所有者,然后调用[myWindowController showWindow:nil]
.
回答by Kevin Snow
That is probably caused by not keeping a strong reference to the created NSWindowController. If you don't keep it, the object will be deleted.
这可能是由于没有保持对创建的 NSWindowController 的强引用造成的。如果您不保留它,该对象将被删除。