我采用了一个scrollview,我正在动态添加看起来像这样的标签
for(int tempvariable=0; tempvariable<n;tempvariable++)
{
CGRect labelFrame2 = CGRectMake( x3,y3, 20, 20 );
UILabel *label3=[[UILabel alloc]initWithFrame:labelFrame2];
[subview addSubview:label3];
}
[self.scrollView addSubview:subview];
"n"
是将标签添加n次到子视图后的动态值我已将子视图添加到scrollview。现在我要在NSLog(@'");
如果我想更改标签的某些标题,而不是仅通过没有子视图的scrollview来更改标签?
答案 0 :(得分:2)
for (UIView *subview in self.scrollView.subviews)
{
if ([subview isKindOfClass:[UILabel class]])
{
UILabel* l=(UILabel)subview;
NSLog(@"%@",l.text);
l.text=@"new title";
}
}
答案 1 :(得分:0)
您可以设置标记以查找要更改标签文本的标签..
for(int tempvariable=0; tempvariable<n;tempvariable++)
{
CGRect labelFrame2 = CGRectMake( x3,y3, 20, 20 );
UILabel *label3=[[UILabel alloc]initWithFrame:labelFrame2];
label3.tag = tempVariable;
// you can set the label text here..
[subview addSubview:label3];
}
正如@tillerstarr在他的回答中提到的,你可以迭代你的子视图来获得标签的文本。
for (UIView *subview in self.scrollView.subviews)
{
if ([subview isKindOfClass:[UILabel class]])
{
if (subview.tag == 5) {
UILabel* l=(UILabel)subview;
// you can check the tag value and change the label title as like this
l.text=@"fifth label";
}
}
}