0

I have 18 UIImageViews declared in my .h, named as such: view1, view2, view3...all the way to view18. How can I, in a loop with iterator i, access the view named view[i]?

so like this:

for(int i=0;i<19;i++)
       imageView=view[i]; //this doesn't work obviously, but how would I do this?
Snowman
  • 31,411
  • 46
  • 180
  • 303

2 Answers2

1

Put them all in an array, and loop though the array. If they are outlets, you could use an IBOutletCollection.

jrturton
  • 118,105
  • 32
  • 252
  • 268
1

Either you're building the views in a loop, and in that case you could add them to an array as you create them. In the other case, you're creating your eighteen views in Interface Builder, in which case jrturton's recommendation about IBOutletCollection is the way to go.

There is no Obj-C introspection feature that lets you find class members by string.

You could grab the getters by synthesizing getters, and calling the getters by building a ("view%d", i) string. With such a string, you can extract a selector and call it.

Needless to say, that would be obscenely complicated compared to adding them into an array (possibly by just filtering [self subviews]).

Ivan Milles
  • 314
  • 1
  • 9