如何从代码加载 nib 文件?
我在界面生成器中创建了一个自定义视图,其中有一些按钮。我在代码中为它创建了一个类作为“文件所有者”,以将按钮连接到操作方法。
那我该如何使用这个类呢?
我不能这样做......
StartScreen *ss = [[StartScreen alloc] initWithFrame: ...];
[self.window.contentView addSubView: ss];
...
因为这只会产生一个空的视图。 (当然:StartScreen 类还不知道有关 nib 文件的任何信息。)
我想做的是:
StartScreen *ss = LoadCustomViewFromNib(@"StartScreen");
[self.window.contentView addSubView: ss];
或者也许我必须
[self iWannaBeANibWithName: @"StartScreen"];
在 StartScreen 的构造函数中说类似的话?
请帮忙... (顺便说一句,我正在为 Mac OS X 10.6 开发)
I created a custom view in interface builder with a few buttons in it. I created a class in code for it as the "Files owner" to connect the buttons to action methods.
How do I use this class then?
I cannot just do it like this...
StartScreen *ss = [[StartScreen alloc] initWithFrame: ...];
[self.window.contentView addSubView: ss];
...
because this only produces an empty view. (of course: the StartScreen class doesn't know anything about the nib file yet.)
What I want to do is something like:
StartScreen *ss = LoadCustomViewFromNib(@"StartScreen");
[self.window.contentView addSubView: ss];
or maybe i have to say something like
[self iWannaBeANibWithName: @"StartScreen"];
in the constructor of StartScreen?
pls help...
(btw I am developing for Mac OS X 10.6)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种选择是使
StartScreen
成为NSViewController
的子类,也许将其名称更改为StartScreenController
。如果您的 nib 文件中有 IBActions 和/或您想将视图控制代码放在它自己的类中,那么这可能是一个更加模块化的解决方案。StartScreenController
声明为NSViewController
的子类StartScreenController
中声明IBOutlets
view
出口连接到视图对象,并在需要时连接其他出口然后:
如果您不使用垃圾回收,请不要忘记释放
ss
当不再需要时。One option is to make
StartScreen
a subclass ofNSViewController
, maybe changing its name toStartScreenController
. This is a potentially more modular solution in case you haveIBActions
in your nib file and/or you want to place view controlling code in its own class.StartScreenController
as a subclass ofNSViewController
IBOutlets
inStartScreenController
if neededStartScreenController
view
outlet to the view object, and other outlets if neededThen:
If you’re not using garbage collection, don’t forget to release
ss
when it’s not needed any longer.Nib 加载函数是 NSBundle 类的一部分。您可以像这样使用它...
请参阅 NSBundle 添加参考。
The Nib loading functions are part of the
NSBundle
class. You can use it like this...See NSBundle Additions reference.