如何从代码加载 nib 文件?

发布于 2024-11-07 12:46:52 字数 611 浏览 0 评论 0原文

我在界面生成器中创建了一个自定义视图,其中有一些按钮。我在代码中为它创建了一个类作为“文件所有者”,以将按钮连接到操作方法。

那我该如何使用这个类呢?

我不能这样做......

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

套路撩心 2024-11-14 12:46:52

一种选择是使 StartScreen 成为 NSViewController 的子类,也许将其名称更改为 StartScreenController。如果您的 nib 文件中有 IBActions 和/或您想将视图控制代码放在它自己的类中,那么这可能是一个更加模块化的解决方案。

  1. StartScreenController 声明为 NSViewController 的子类
  2. 如果需要,在 StartScreenController 中声明 IBOutlets
  3. 将 nib 文件的所有者类设置为 < code>StartScreenController
  4. 将文件的所有者 view 出口连接到视图对象,并在需要时连接其他出口

然后:

StartScreenController *ss = [[StartScreenController alloc] initWithNibName:@"nibname" bundle:nil];    
[self.window.contentView addSubView:ss.view];
…

如果您不使用垃圾回收,请不要忘记释放 ss 当不再需要时。

One option is to make StartScreen a subclass of NSViewController, maybe changing its name to StartScreenController. This is a potentially more modular solution in case you have IBActions in your nib file and/or you want to place view controlling code in its own class.

  1. Declare StartScreenController as a subclass of NSViewController
  2. Declare IBOutlets in StartScreenController if needed
  3. Set the nib file’s owner class to be StartScreenController
  4. Connect the file’s owner view outlet to the view object, and other outlets if needed

Then:

StartScreenController *ss = [[StartScreenController alloc] initWithNibName:@"nibname" bundle:nil];    
[self.window.contentView addSubView:ss.view];
…

If you’re not using garbage collection, don’t forget to release ss when it’s not needed any longer.

燃情 2024-11-14 12:46:52

Nib 加载函数是 NSBundle 类的一部分。您可以像这样使用它...

@implementation StartScreen
- (id) init {
    if ((self = [super init])) {
        if (![NSBundle loadNibNamed:@"StartScreen" owner:self])
            // error
        // continue initializing
    }
    return self;
}

请参阅 NSBundle 添加参考

The Nib loading functions are part of the NSBundle class. You can use it like this...

@implementation StartScreen
- (id) init {
    if ((self = [super init])) {
        if (![NSBundle loadNibNamed:@"StartScreen" owner:self])
            // error
        // continue initializing
    }
    return self;
}

See NSBundle Additions reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文