简体   繁体   English

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

[英]Objective-C Using custom XiB in another XiB

I've really spent my hours for finding any approach about this problem. 我真的花了很多时间来找到解决这个问题的方法。

I have; 我有;

CustomView.h that extends from UIView and is also IB_DESIGNABLE CustomView.m for implementation of this view with override methods init , initWithCoder and initWithFrame CustomView.xib that bound to CustomView class with all properties And, I have : CustomView.h ,它从UIView扩展而来,也是IB_DESIGNABLE CustomView.m,用于实现这个视图,使用覆盖方法initinitWithCoderinitWithFrame CustomView.xib绑定到具有所有属性的CustomView类,我有:

  • CustomTableViewCell.h that extends from UITableViewCell 从UITableViewCell扩展的CustomTableViewCell.h
  • CustomTableViewCell.m that implementation of this cell CustomTableViewCell.m表示执行此单元格
  • CustomTableViewCell.xib that bound to CustomTableViewCell class with all properties&outlets. CustomTableViewCell.xib绑定到具有所有属性和出口的CustomTableViewCell类。

CustomTableViewCell.xib includes CustomView... CustomTableViewCell.xib包含CustomView ...

There is no any error, but the area of CustomView remains blank. 没有任何错误,但CustomView的区域仍为空白。

For view you need to do some work around. 对于视图,您需要做一些解决方法。

Create a property and link your view in the XIB to it; 创建一个属性并将您在XIB的视图链接到它;

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

Then have something like this: 然后有这样的事情:

- (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;

}

Basically you have to load the XIB manually and add it to your custom view. 基本上,您必须手动加载XIB并将其添加到自定义视图中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM