Indexed UILabels Objective-C
i am prog开发者_如何学Pythonramming an iphone application and need help with the UILabels. An example: I have 10 Labels named Label1, Label2, Label3 etc. and Label1.text = @"1", Label2.text=@"2" etc. is there a way to do it in a for-loop. Like for(int i = 1, i<11,i++){Labeli.text = ...} ? thx for helping.
Set tag for each label and access the value as follows,
for(int i=1; i<=10;i++)
{
UILabel *lab=(UILabel *)[self.view viewWithTag:i];
[lab setText:[NSString stringWithFormat:@"%d",i]];
}
or else do as follows,
NSArray *labels=[NSArray arrayWithObjects: label1, label2, nil];
int i=1;
for(UILabel *label in labels) {
[label setText:[NSString stringWithFormat:@"%d",i]];
i++;
}
Here's the proper for loop:
for(int i = 0; i < numLabels; i++) {
UILabel *label = [[UILabel alloc] init];
label.text = [NSString stringWithFormat:@"%i", i];
// other label customizations here
[myMutableArray addObject:label]
[label release];
}
Then, later, access each label with [myMutableArray objectAtIndex:index];
精彩评论