[英]How to inherit a custom view which has a .xib file
There is a custom view, that consist of class and xib file. 有一个自定义视图,由类和xib文件组成。 I use it as
我用它
[[NSBundle mainBundle] loadNibNamed:@"myViewNib" owner:self options:nil];
Question is how to inherit this custom view. 问题是如何继承此自定义视图。
I inherit the myViewClass, but then I can't use the code above to create a instance.I think I'm in the wrong way. 我继承了myViewClass,但是后来我无法使用上面的代码创建实例,我认为我的方法是错误的。
One option is not to create MYBaseView
itself in the .xib file, but it's subview. 一种选择是不在
MYBaseView
文件中创建MYBaseView
本身,而是子视图。 This way you will be able to inherit MYBaseView
easily. 这样,您将能够轻松继承
MYBaseView
。
This is the code should work correctly if you create your view object programmatically or in the Interface Builder: 如果您以编程方式或在“界面生成器”中创建视图对象,则此代码应该可以正常工作:
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self setUp];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setUp];
}
return self;
}
- (void)setUp
{
UIView *view = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil] lastObject];
[self addSubview:view];
}
它应该是:
myViewClass *newView = [[NSBundle mainBundle] loadNibNamed:@"myViewNib" owner:self options:nil];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.