Basically my view controller has 49 different instance variables of class CircleView (called circleView1, circleView2, …, circleView49). The CircleView class is a simple subclass of UIImageView and basically returns a circle picture when initialized.
Here is what I am trying to do:
In my implementation for the view controller, I want to be able to select the instance variable using a string, for example let’s say I generate a random integer i between 1 and 49, lets say it’s 5. I would like to be able to build an NSString using something like [NSString stringWithFormat @"circleView%i",i]; and somehow reach the actual circleView5 instance variable. Is this possible?
P.S. I’m looking for something in the lines of NSClassFromString, but which applies to instance variables or objects.
KVC is your friend. Assuming you have a string with the name of the circleView in it,
you access that property on the controller using KVC:
On a side note, you should consider redesigning the object heirarchary; this will be very difficult to maintain, and there are certainly better ways to do it (an array with the CircleView’s accessed by index would be my first choice).