1

我的视图中有 8 个 UIImageView,全部在.hand中声明.m

我打电话给他们img01,,,,... img02img03img08

我也有 8 张照片,名为01.png, 02.png, 03.png,..., 08.png

如果我想将第一张图片设置为第一imageView张图片,我将使用以下命令:

UIImage *image = [UIImage imageNamed:@"01.png"];
    [img01 setImage:image];

如果我有很多UIImageViews图片怎么办?

我尝试使用此代码,但不知道如何执行最后一行代码。

int count = 1;
while (count <= 8) {

    NSString *countString = [[NSString alloc]initWithFormat:@"%i", count];

    NSString *picname = [NSString stringWithFormat:@"0%@.png", countString];

    NSString *imgName = [NSString stringWithFormat:@"img0%@", countString];

    UIImage *image = [UIImage imageNamed:picname];

    [img01 setImage:image];

    count++;

}

该代码只会更改第一个 imageView。我需要它来改变所有的imageViews.

4

1 回答 1

1

创建一个NSMutableArray,并用 UIImageViews 填充它。目前您只设置第一个:

[img01 setImage:image];

你可以这样做:

NSMutbaleArray imageViews = [[NSMutableArray alloc] initWithCapacity:8];
for (int i = 0; i < 8; ++i) {
    NSString *countString = [NSString stringWithFormat:@"%i", count];
    NSString *picname = [NSString stringWithFormat:@"0%@.png", countString];
    NSString *imgName = [NSString stringWithFormat:@"img0%@", countString];
    UIImage *image = [UIImage imageNamed:picname];
    UIImageView * view = [[[UIImageView alloc] init] autorelease];
    [view setImage:image];
    [imageViews addObject:view];
}
于 2012-04-21T16:25:00.247 回答