2013-07-26 71 views
1

我有一个简单的for循环,它改变了一组9个UIImageView中的图像。不过,我有一个问题,我无法在for循环中配合更改UIImageView的名称,因此最终结果是for循环仅影响我iOS应用中的9个UIImageView中的1个。NSString通过for循环编辑 - iOS

这里是我的代码:

for (current_photo = 1; current_photo < 10; current_photo++) { 
     picview_1.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@%i.png", SERVER_URL, current_photo]]; 
    } 

你会发现,在我的代码有一点:

picview_1.image 

当我更换 “PIC view_1” 其中 “picview_current_photo”,说到与错误。

我在做什么错?请解释。

感谢您的时间:)

回答

2

创建的UIImageView秒的阵列,并且通过索引访问它们在一个循环中,像这样:

NSArray *imageViews = @[picview_1, picview_2, picview_3, picview_4, ..., picview_9]; 
// Arrays are indexed from zero, so I changed the index to run from 0 to 9 
for (current_photo = 0; current_photo < 9; current_photo++) { 
    // Since current_photo is zero-based, I added one to it in stringWithFormat 
    UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"%@%i.png", SERVER_URL, current_photo+1]]; 
    [[imageViews objectAtIndex:current_photo] setImage:img]; 
} 
+0

谢谢。一流的解决方案。而且我还是一名初学者程序员,所以使用Array并没有想到。 – Supertecnoboff

0

你可能是最好建立一个数组UIImageViews。然后从该数组访问您需要的UIImageView并根据需要设置图像。