我用一堆标签创建了Custom UIView。 label1,label2,
UIView *testContentView = [[[UINib nibWithNibName:@"testContentView" bundle:nil] instantiateWithOwner:nil options:nil] objectAtIndex:0];
如何访问标签和设置文字?
答案 0 :(得分:2)
您需要在界面上定义它们。
@interface TestContentView : UIView
@property (weak, nonatomic) IBOutlet UILabel *label1;
@property (weak, nonatomic) IBOutlet UILabel *label2;
@end
然后......
testContentView.label1.text = @"foo";
testContentView.label2.text = @"bar";
修改强>
由于您使用的是NIB,因此在属性中添加了IBOutlet
,您还需要在Interface Builder中将它们连接起来。
答案 1 :(得分:0)
要访问自定义属性,需要将UIView转换为TestContentView。
TestContentView *testContentView = (TestContentView *)[[[UINib nibWithNibName:@"testContentView" bundle:nil] instantiateWithOwner:nil options:nil] objectAtIndex:0];