0

我用一堆标签创建了自定义 UIView。标签1,标签2,

UIView *testContentView = [[[UINib nibWithNibName:@"testContentView" bundle:nil] instantiateWithOwner:nil options:nil] objectAtIndex:0];

如何访问标签和设置文本?

4

2 回答 2

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 中连接这些属性。

于 2013-04-24T19:30:56.610 回答
0

为了访问您的自定义属性,您需要将 UIView 转换为 TestContentView。

TestContentView *testContentView = (TestContentView *)[[[UINib nibWithNibName:@"testContentView" bundle:nil] instantiateWithOwner:nil options:nil] objectAtIndex:0];
于 2013-04-24T20:52:30.287 回答