I have a variable name referencing question for the Objective C gurus out there.
Lets say I have 6 UILabels on a form, the are setup with properties naming them myLabel1 – myLabel6
I would like to go through a for loop and populate these with something depending on the loop but im unsure how to specifiy the for loops variable and make it part of the labels name.
Here is what I would like to do:
for (int LP = 0; i <5)
{
labelLP.text = [NSString stringWithFormat:@"My label number:%d", LP};
}
What im not sure of is how to reference the label and append the LP int and use it in my loop. I’m sure there is a way to do this just not sure how.. Anyone??
I don’t think you can create variable names on the fly, at least not trivially.
You could always use a switch case inside your loop:
You could also store your labels in an array, and loop through that array.
The important point is not to get fixated about the variable names, but to think about why you need your objects and how to get them.