Interface-Builder:将NSView-class与.xib“结合”

时间:2013-08-21 09:29:01

标签: macos interface-builder

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

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

在MyCustomView.xib中,我也将Class设置为MyCustomView。在MyCustomView.m中,系统会调用- (void)awakeFromNib,但- (id)initWithCoder:(NSCoder *)aDecoder- (id) awakeAfterUsingCoder:(NSCoder*)aDecoder不会。{/ p>

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

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

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

enter image description here

此视图应为MyCustomView类型,其中

MyCustomView.h MyCustomView.m MyCustomView.xib

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

如果我这样做

- (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?

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

https://dl.dropboxusercontent.com/u/119600/Testproject.zip,您可以找到一个带MyCustomView的简单项目(不在ViewController中,但在window.xib中) - 但它没有显示MyCustomView.xib中的按钮。我想要实现这一点 - 最简单,最好的方法是什么?

2 个答案:

答案 0 :(得分:18)

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

鉴于文件...

MyCustomView.h
MyCustomView.m
MyCustomView.xib

在MyCustomView.h中

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

    @property (nonatomic, strong) IBOutlet NSView *view;

在MyCustomView.xib

  • 确保只有一个顶级视图
  • 在Identity Inspector中将文件的所有者类设置为MyCustomView
  • 确保将顶级视图设置为默认的NSView类。
  • 现在您可以将MyCustomView.h中声明的IBOutlet连接到xib文件中的接口对象。至少您需要将顶级视图连接到view插座。

在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。

在10.8之前的OSX中

方法loadNibNamed是一个类方法 - 如果你需要向后兼容性,请使用它,但现在不推荐使用它:

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

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

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

    MyCustomView.h中的

  • 。添加NSView属性

  • MyCustomView.xib中的

  • 。将顶级视图从MyCustomView自定义类更改为NSView(默认值)
    。将文件所有者设置为MyCustomView 。将IBOutlets从文件所有者的viewmyCustomButton连接到界面视图和按钮
    。进行测试使视图变得更小,并将按钮向上推到右上角(你不会在窗口中看到它,因为它在这里)

  • MyCustomView.m中的

  • 。使用此处的initWithFrame方法

  • 替换所有实施代码

答案 1 :(得分:0)

为了加载视图或控件的自定义子类(或者可以在Interface Builder中使用的任何其他类),您需要添加基本版本(在您的情况下为NSView,并且因为您有完成),然后在窗口中选择该对象并转到Identity Inspector(Cmd-Opt 3)。

而不是Class的预定义值(在您的情况下为NSView),键入自定义子类的名称。瞧!

与IB UX相关的一个小细节是,您可能必须从输入字段移动焦点,以便在构建和运行应用程序时注册该更改。