How to inherit a custom view which has a .xib file

六月ゝ 毕业季﹏ 提交于 2019-12-08 17:47:28

One option is not to create MYBaseView itself in the .xib file, but it's subview. This way you will be able to inherit MYBaseView easily.

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];
}

It should be:

myViewClass *newView = [[NSBundle mainBundle] loadNibNamed:@"myViewNib" owner:self options:nil];
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!