我正在尝试将UILabel插入到数组中,然后在屏幕上的某个点显示它。使用for循环,我计算数组中的元素数量,对于每个元素,我想创建一个标签。这只是生成一个标签(数组中的第一个元素)。
int y = 260;
for(int i = 0; i < _jsonArray.count; i ++)
{
NSNumber *one = [[_jsonArray objectAtIndex:i] objectForKey:@"amount"];
NSString *name = [[_jsonArray objectAtIndex:i] objectForKey:@"name"];
UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, y, 200, 40)];
[myLabel setBackgroundColor:[UIColor clearColor]];
[myLabel setText:name];
[_slices addObject:one];
[[self view] addSubview:myLabel];
y=y+20;
}
非常感谢任何帮助!
答案 0 :(得分:1)
使arrayLabels成为一个属性,以便您可以访问视图控制器中的任何位置
_arrayLabels = [NSMutableArray array];
int y = 260;
for(int i = 0; i < _jsonArray.count; i ++)
{
NSNumber *one = [[_jsonArray objectAtIndex:i] objectForKey:@"amount"];
NSString *name = [[_jsonArray objectAtIndex:i] objectForKey:@"name"];
UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, y, 200, 40)];
[myLabel setBackgroundColor:[UIColor clearColor]];
[myLabel setText:name];
[_slices addObject:one];
[[self view] addSubview:myLabel];
[_arrayLabels addObject:myLabel];
y=y+20;
}
然后您可以使用(UILabel *)[_arrayLabels objectAtIndex:i];