我想在 Interface-Builder 中设置一个自定义 NSView,但我无法让它在 OSX 上工作。
在我的 ViewController 的 .xib 中,我添加了一个自定义视图并将类设置为 MyCustomView。我创建了 MyCustomView.h、MyCustomView.m 和 MyCustomView.xib。
在 MyCustomView.xib 中,我也将类设置为 MyCustomView。在MyCustomView.m中,- (void)awakeFromNib
被称为,但是- (id)initWithCoder:(NSCoder *)aDecoder
and - (id) awakeAfterUsingCoder:(NSCoder*)aDecoder
aren't.
我想要实现的是,在我的 ViewController 中,我添加的视图被我在 MyCustomView.xib 中设置的视图“填充”。最好的方法是什么?
编辑:我认为我还不够清楚......
我的 ViewController 包含一个名为 MyCustomView 的自定义视图。
该视图应该是 MyCustomView 类型,其中
MyCustomView.h
我的自定义视图.m
我的自定义视图.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:我上传了一个简单的项目
At https://dl.dropboxusercontent.com/u/119600/Testproject.zip https://dl.dropboxusercontent.com/u/119600/Testproject.zip您会发现一个带有 MyCustomView 的简单项目(不是在 ViewController 中,而是在 window.xib 中) - 但它没有显示 MyCustomView.xib 中的按钮。我想要实现这一目标 - 最简单、最好的方法是什么?