I added 6 UIImageViews in Interface Builder.
Those are declared.
@property (nonatomic, strong) IBOutlet UIImageView *Image1;
@property (nonatomic, strong) IBOutlet UIImageView *Image2;
@property (nonatomic, strong) IBOutlet UIImageView *Image3;
@property (nonatomic, strong) IBOutlet UIImageView *Image4;
@property (nonatomic, strong) IBOutlet UIImageView *Image5;
@property (nonatomic, strong) IBOutlet UIImageView *Image6;
Those UIImageView’ name has a rule – “Image” + number.
I want to select those ImageViews dinamically.
For example,
for (NSInteger i = 0; i < 6 ; i++) {
if(... condition ) { //new [[NSString stringWithFormat:@"Image%d", i+1] setHidden:YES]; //--(1) } else { [[NSString stringWithFormat:@"Image%d", i+1] setHidden:NO]; //--(2) } }
But, this code isn’t correct.
Please tell me more good way.
jonkroll’s suggestion to put your image views in an array is a good way to do it, and generally the highest performance.
Another way is to use key-value coding (KVC) to access your properties by name:
Using the view tag, as Mark suggests, is a third way to do it. His answer is a little short on details, so I will provide some.
You can set the tag in your nib:
So you can set the tag of your
Image1image view to 1, and the tag of yourImage2image view to 2, and so on.Then you can find an image view by its tag using the
viewWithTag:method on your top-level view: