本文介绍了Interface-Builder:“组合”带有.xib的NSView类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Interface-Builder中设置一个自定义的NSView,但我不能让它适用于OSX。

I'd like to set up a custom NSView in Interface-Builder, but I don't get it to work for OSX.

在我的ViewController的.xib中,我添加了一个自定义视图并将Class设置为MyCustomView。我创建了MyCustomView.h,MyCustomView.m和MyCustomView.xib。

In my ViewController's .xib, I added a custom view and set the Class to MyCustomView. I created MyCustomView.h, MyCustomView.m and MyCustomView.xib.

在MyCustomView.xib中,我也将Class设置为MyCustomView。在MyCustomView.m中,调用了 - (void)awakeFromNib ,但 - (id)initWithCoder:(NSCoder *)aDecoder - (id)awakeAfterUsingCoder:(NSCoder *)aDecoder 不是。

In MyCustomView.xib, I set the Class to MyCustomView as well. In MyCustomView.m, - (void)awakeFromNib is called, but - (id)initWithCoder:(NSCoder *)aDecoder and - (id) awakeAfterUsingCoder:(NSCoder*)aDecoder aren't.

我想要什么要实现的是,在我的ViewController中,我添加的视图被填充了我在MyCustomView.xib中设置的视图。最好的方法是什么?

What I'd like to achieve is that in my ViewController, the view I added is "filled" with the view I set up in MyCustomView.xib. What's the best way to do that?

编辑:我认为我不够清楚......

我的ViewController包含一个名为MyCustomView的自定义视图。

I've got my ViewController containing a Custom View called MyCustomView.

此视图应为MyCustomView类型,其中

This view should be of type MyCustomView, where

MyCustomView.h
MyCustomView.m
MyCustomView.xib

MyCustomView.hMyCustomView.mMyCustomView.xib

存在。我已经将MyCustomView.xib的文件所有者设置为MyCustomView,我已经将ViewController中的CustomView设置为MyCustomView - 但它不起作用。

exists. I already set the File's Owner of MyCustomView.xib to MyCustomView and I already set the CustomView in my ViewController to MyCustomView - but it doesn't work.

如果我这样做with

If I do it with

- (void)awakeFromNib {
    NSString* nibName = NSStringFromClass([self class]);
    NSArray* topLevelObjects;
    [[NSBundle mainBundle] loadNibNamed:nibName
                              owner:nil
                    topLevelObjects:&topLevelObjects];

    NSView* view = topLevelObjects[0];
    [view setFrame:[self bounds]];
    [self addSubview:view];
}

我只获得NSView类型的视图,而不是MyCustomView ...是否存在没有简单的方法告诉ViewController.xib它是一个MyCustomView?

I only get a view of type NSView, not MyCustomView... Is there no easy way to tell the ViewController.xib that it's a MyCustomView?

编辑2:我上传了一个简单的项目

在你找到一个带MyCustomView的简单项目(不是在ViewController中,而是在window.xib中) - 但是它没有显示MyCustomView.xib中的按钮。我想要实现这一点 - 最简单,最好的方式是什么?

At https://dl.dropboxusercontent.com/u/119600/Testproject.zip you find a simple project with the MyCustomView (not in a ViewController but in the window.xib) - but it doesn't show the button which is in MyCustomView.xib. I'd like to achieve exactly that - what's the simplest, best way?

推荐答案

编辑 - 道歉,我现有的答案没有考虑到连接出口和行动的必要性。这种方式应该这样做...

EDIT - apologies, my existing answer failed to take into account the need to connect outlets and actions. This way should do it...

给定文件......

Given the files...

MyCustomView.h
MyCustomView.m
MyCustomView.xib

In MyCustomView.h

In MyCustomView.h


  • 为您的界面元素声明IBOutlets。您至少需要一个,指向xib文件中顶级视图的指针

  • declare IBOutlets for your interface elements. You need at least one, to hold a pointer to the top-level view in the xib file

@property(非原子,强)IBOutlet NSView *查看;

在MyCustomView.xib中

In MyCustomView.xib


  • 确保在Identity Inspector中只有一个顶级视图

  • 将文件的所有者类设置为MyCustomView

  • 确保顶级视图设置为默认 NSView 类。

  • 现在您可以连接声明的IBOutlets在MyCustomView.h中接口xib文件中的对象。至少您需要将顶级视图连接到视图插座。

  • ensure that there is only one top-level view
  • set file's owner class to MyCustomView in the Identity Inspector
  • ensure that the top-level view is set to the default NSView class.
  • now you can connect IBOutlets declared in MyCustomView.h to interface objects in the xib file. At very least you need to connect up the top-level view to your view outlet.

在MyCustomView.m中:

In MyCustomView.m:

- (id)initWithFrame:(NSRect)frame
{
    NSString* nibName = NSStringFromClass([self class]);
    self = [super initWithFrame:frame];
    if (self) {
        if ([[NSBundle mainBundle] loadNibNamed:nibName
                                          owner:self
                                topLevelObjects:nil]) {
            [self.view setFrame:[self bounds]];
            [self addSubview:self.view];
            [self.myCustomButton setTitle:@"test success"];
        }
    }
    return self;
}

在窗口的xib文件中,添加自定义NSView并将其类更改为MyCustomView 。

In your window's xib file, add a custom NSView and change it's class to MyCustomView.

在OSX 10.8之前的方法 loadNibNamed 是一个类方法 - 如果你需要向后兼容,请使用它,但现在已弃用:

in OSX prior to 10.8 the method loadNibNamed was a class method - use it instead if you need backwards compatibility, but it is deprecated now:

[NSBundle loadNibNamed:@"NibView" owner:self]

请注意,MyCustomView.xib的视图是 NOT MyCustomView的视图,但它是视图的唯一子视图(这是类似于tableViewCell拥有单个contentView的方式。)

Note that MyCustomView.xib's view is NOT MyCustomView's view, but the sole subview of it's view (this is similar to the way a tableViewCell possesses a single contentView).

在您发布的项目示例中,您需要进行以下更改:

In the project sample you have posted, you need to make the following changes:


  • 在MyCustomView.h中

    。在MyCustomView.xib中添加NSView属性

  • in MyCustomView.h
    . add an NSView property



。将顶级视图从 MyCustomView 自定义类更改为 NSView (默认值)

。将文件所有者设置为 MyCustomView

。将IBOutlets从File的所有者的视图 myCustomButton 连接到界面视图和按钮

。用于测试使视图更小并将按钮向上推到右上方(您将不会在窗口中看到它,因为它在这里)

in MyCustomView.xib:
. change the top-level view from MyCustomView custom class to NSView (the default)
. set the File's Owner to MyCustomView.
. connect IBOutlets from File's owner's view and myCustomButton to interface view and button
. for testing make the view a lot smaller and push the button up to the top right (you won't see it in your window as it is here)

在MyCustomView.m中:

。用 initWithFrame 方法替换所有实现代码

in MyCustomView.m:
. replace all of your implementation code with the initWithFrame method here

这篇关于Interface-Builder:“组合”带有.xib的NSView类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-11 14:41