views:

223

answers:

1

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!

+2  A: 

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".)

Chuck
I think you should be able to... for example, this is the same situation solved in Delphi.. (Looking for something similar to FindComponent)Instead of writing:Edit1.Text := 'Text 1';Edit2.Text := 'Text 2';Edit3.Text := 'Text 3';Edit4.Text := 'Text 4';{...}Edit9.Text := 'Text 9';{...it's easier to write }{ Use the forms FindComponent to find a component on the form. TypeCast the Result of FindComponent to the TComponent to be able to use it.}for i := 1 to 9 do TEdit(FindComponent('Edit'+IntToStr(i))).Text := 'Text' + IntToStr(i);
Delphi is not C.
Chuck
Based on the limitation I think you should word that as Objective C is not Delphi. I'm asking more experience iphone than me, how something similar could be done in Objective C. Where there's a will there's probably a way.
And the way is an array.
Chuck
Rick: Objective-C is C with extra object-oriented extensions.
rpetrich
Seems silly to me to have to shove everything into an array just to then access the object. If you can programatically build the name of an object into a string, you should then be able to access it. I was able to simulate what I needed by just using the Tag field. In effect the Tag is just another name for a object which I can then build into any String and use withViewTag to access it. I'm sure there's a better way to get what I'm after, but for now, it works.Thanks!
You don't have to shove things into an array to access them. But it is the idiomatic thing to do if you want to loop through them sequentially, which is what you wanted to do.
Chuck
Chuck, I call a truce :-) But, I do thank you for your input. It made me do more digging and thinking.