【发布时间】:2013-08-23 14:35:38
【问题描述】:
我想在 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 没有被调用。
我想要实现的是,在我的 ViewController 中,我添加的视图被我在 MyCustomView.xib 中设置的视图“填充”。最好的方法是什么?
编辑:我认为我不够清楚......
我的 ViewController 包含一个名为 MyCustomView 的自定义视图。
这个视图应该是 MyCustomView 类型,其中
MyCustomView.h 我的自定义视图.m MyCustomView.xib
存在。我已经将 MyCustomView.xib 的 File's Owner 设置为 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 中的按钮。我想实现这一点 - 最简单、最好的方法是什么?
【问题讨论】:
-
你想完成什么?这是一套标准控件,您需要使用的地方很多吗?视图的各个部分是否需要触发或需要从
ViewController访问的出口?视图类真的更像是一个子视图控制器吗?另外,上面列出的-awakeFromNib代码在哪里(MyCustomClass或ViewController)? -
请看我上传的项目,应该会更清楚。
awakeFromNib在MyCustomClass中。