[英]programmatically add a bunch of custom view from XIB to UIStackView
我有一个TagDisplayCell.xib及其关联的TagDisplayCell.m。 在.m
文件中,我像这样加载视图。
- (id)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder: aDecoder];
if(self){
//load the interface file from .xib
[[NSBundle mainBundle] loadNibNamed:@"TagDisplayCell" owner:self options:nil];
//add as subview
[self addSubview:self.view];
self.view.translatesAutoresizingMaskIntoConstraints = NO;
[self addConstraint:[self pin:self.view attribute:NSLayoutAttributeTop]];
[self addConstraint:[self pin:self.view attribute:NSLayoutAttributeLeft]];
[self addConstraint:[self pin:self.view attribute:NSLayoutAttributeBottom]];
[self addConstraint:[self pin:self.view attribute:NSLayoutAttributeRight]];
}
return self;
}
在我的视图控制器中,有一个垂直的UiStackView
(以便将内容堆叠成行)。 我想将TagDisplayCell
几个实例添加到此UiStackView。 这是我在做什么。
//create all the views
for(int i=0; i<[commentsAndTagsArray count]; i++)
{
NSObject *obj =commentsAndTagsArray[i];
if ([obj isKindOfClass:[Tag class]])
{
Tag *tempTag = ((Tag*) obj);
TagDisplayCell *tempTagCell = [[TagDisplayCell alloc] initWithFrame:frame];
[uiStackView addSubview:tempTagCell];
}
}
当我运行时,屏幕上没有任何显示。 我在这里想念什么?
在View子类上调用initWithFrame:
时,不会从XIB加载它。
以与第一个代码段相同的方式执行视图的加载:
TagDisplayCell *tempTagCell = (TagDisplayCell *)[[[NSBundle mainBundle] loadNibNamed:@"TagDisplay" owner:nil options:nil] firstObject];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.