Identifying the name of a class during run time
So I have run into a fairly complex problem with my application. I have about 40 UIImageView objects that need to be displayed at different locations when different ones are stacked (EX img 1 needs to take img 2 place if img2 ==NO).
What I am trying to do is use a "for loop" and run through all my Boolean values that check if the image is selected. I was curious if it is at all possible to call variable like this
-(IBAction) button:(id) sender{
scrollView.hidden = YES;
NSInteger i = 1;
(@"imagenumber%d", i) = [[UIImageView alloc] initWithFrame: CGRectMake (28, 230, 86, 26)];
[[imagenumber(@"%d", i)] setImage:[UIImage imageNamed:@"csa.jpg"]];
[self.view addSubview: [imagenumber(@"%d", i)]];}
}
where @"imagenumber%d" is my NSImageView and i is a number ranging from 1-40.
I'm sorry if this is confusing, I really would appreciate any help at all on the subject because I can’t seem to get any idea and have been working on this problem for several开发者_如何学Python weeks.
Put the views in a NSMutableArray
- you can access elements in those by index, use fast enumeration, ... E.g.:
UIImageView *image = [[UIImageView alloc] ...
[myMutableArray addObject:image];
// ...
For the sake of hilarity you could also put them in a intrinsic array and use pointer arithmetic. (Which is probably what happens behind the scenes with faster enumeration for those interested)
UIImageView * imageArray[40];
...
for(UIImageView * this = imageArray; this; this++)
{
this = [[UIImageView alloc] initWithFrame: CGRectMake (28, 230, 86, 26)];
...
}
精彩评论