Rick
Rick

Reputation:

Using The value of an NSString variable to manipulate the object the the name refers to

I have a bunch of images on the screen.... UIImageView *s1, s2 ,s3 etc up to *s10 Now suppose I want to update the image each displays to the same image. Rather than doing s1.image = sampleimage; s2.image = sampleimage; : s10.image = sampleimage;

How could i write a for loop to go from 1 to 10 and then use the loop var as part of the line that updates the image. Something like this. for ( i = 1; i <- 10; ++i ) s(i).image = sample; // I know that does not work

Basic question is how do I incorporate the variable as part of the statement to access the image? Don't get hung up on my example. The main question is how to use a variable as part of the access to some element/object.

Bottom Line... If I can build the name of a UIImageView into a NSString object, How can I then use that NSString object to manipulate the UIImageView.

Thanks!

Upvotes: 0

Views: 463

Answers (1)

Chuck
Chuck

Reputation: 237020

You can't. That is not the name of the UIImageView — it's the name of a variable that refers to the image view, and those variables do not necessarily even exist at runtime.

It sounds like what you want is an array — either an NSArray or UIImageView *s[10]. (This is assuming there aren't actually more descriptive names you could give the views than "s1" through "s10".)

Upvotes: 2

Related Questions