如何继承具有.xib文件的自定义视图 [英] How to inherit a custom view which has a .xib file

查看:210
本文介绍了如何继承具有.xib文件的自定义视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个自定义视图,由类和xib文件组成。我使用它作为

There is a custom view, that consist of class and xib file. I use it as

[[NSBundle mainBundle] loadNibNamed:@"myViewNib" owner:self options:nil];

问题是如何继承此自定义视图。

Question is how to inherit this custom view.

我继承myViewClass,但是我不能使用上面的代码创建一个实例。我想我的方式错误。

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.

推荐答案

一个选项不是在.xib文件中创建 MYBaseView
这样你就可以轻松地继承 MYBaseView

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

这篇关于如何继承具有.xib文件的自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆