我用一堆标签创建了自定义 UIView。标签1,标签2,
UIView *testContentView = [[[UINib nibWithNibName:@"testContentView" bundle:nil] instantiateWithOwner:nil options:nil] objectAtIndex:0];
如何访问标签和设置文本?
我用一堆标签创建了自定义 UIView。标签1,标签2,
UIView *testContentView = [[[UINib nibWithNibName:@"testContentView" bundle:nil] instantiateWithOwner:nil options:nil] objectAtIndex:0];
如何访问标签和设置文本?
您需要在界面上定义它们。
@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 中连接这些属性。
为了访问您的自定义属性,您需要将 UIView 转换为 TestContentView。
TestContentView *testContentView = (TestContentView *)[[[UINib nibWithNibName:@"testContentView" bundle:nil] instantiateWithOwner:nil options:nil] objectAtIndex:0];