Objective-C:在另一个XiB中使用自定义XiB

3
我真的花了很多时间寻找解决这个问题的方法。
我有:
- CustomView.h,它扩展自UIView并且也是IB_DESIGNABLE的。 - CustomView.m,用于实现此视图,重写了init、initWithCoder和initWithFrame方法。 - CustomView.xib,绑定到CustomView类,具有所有属性。 - CustomTableViewCell.h,它扩展自UITableViewCell。 - CustomTableViewCell.m,实现此单元格。 - CustomTableViewCell.xib,绑定到CustomTableViewCell类,具有所有属性和outlets。
CustomTableViewCell.xib包括CustomView...
没有任何错误,但CustomView的区域仍然是空白的。
1个回答

2

查看视图需要进行一些操作。

创建一个属性并将您的视图链接到XIB中;

@property (strong, nonatomic) IBOutlet UIView *xibView;

然后类似于这样:

- (void)awakeFromNib{
    [super awakeFromNib];
    [self commonInit];
}

- (instancetype)init{
    self = [super init];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit{
    //this will work if the view has the same name as the XIB
    [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
    self.xibView.frame = self.bounds;
    [self addSubview:self.xibView];
    self.xibView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

}

基本上,您需要手动加载XIB并将其添加到自定义视图中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接