如何在iOS系列中动态创建标签

时间:2013-08-13 16:33:40

标签: cocoa-touch

这就是问题,我想在xCode中绘制一个dinamic屏幕,这取决于从webServer传来的标签数量,是这样的......

for (int i = 0; i< [webserver.arraylabels count]; i++){
   UILabel *label[i];
   label[i] = [functions createLabel:[[webserver.arraylabels objectAtIndex:i] textLabel]
                                                   locationX:[webserver.arraylabels objectAtIndex:i] locationX]
                                                   locationY:[webserver.arraylabels objectAtIndex:i] locationY]
                                                 heightControl:[webserver.arraylabels objectAtIndex:i] heightControl]
                                                  widthControl:[webserver.arraylabels objectAtIndex:i] widthControl]
                                                adjustmentControl:UIViewAutoresizingFlexibleRightMargin];

[cell addSubview:label[i]];
}

[functions createLabel ...]是一个函数,它返回一个UILabel对象类型......

如果我运行这个我得到一个错误,因为标签名称上的[i],我该怎么办?

提前感谢您的支持

1 个答案:

答案 0 :(得分:0)

这要归功于H2CO3

NSMutableArray *arregloControles = [[NSMutableArray alloc]init];

for (int i = 0; i< [webserver.arraylabels count]; i++){
   UILabel *control;
   control = [functions createLabel:[[webserver.arraylabels objectAtIndex:i] textLabel]
                                                   locationX:[webserver.arraylabels objectAtIndex:i] locationX]
                                                   locationY:[webserver.arraylabels objectAtIndex:i] locationY]
                                                 heightControl:[webserver.arraylabels objectAtIndex:i] heightControl]
                                                  widthControl:[webserver.arraylabels objectAtIndex:i] widthControl]
                                                adjustmentControl:UIViewAutoresizingFlexibleRightMargin];

[cell addSubview:control];

[arregloControles addObject:control];
}

并将其称为H2CO3建议:

UILabel *label = [arregloControles objectAtIndex:0]; //no casting required

UILabel *label = (UILabel*)[self.view viewWithTag: labelTag];