我有一个包含 4 个不同对象的 NSMutableArray: 还有 4 个 UIImageView:为了从这个数组中获取不同的图像,我编写了一个代码,但它不起作用。请问有什么建议吗?
images = [[NSMutableArray alloc] init];
UIImage *image1 = [UIImage imageNamed:@"ca.png"];
[images addObject:image1];
UIImage *image2 = [UIImage imageNamed:@"ta.png"];
[images addObject:image2];
UIImage *image3 = [UIImage imageNamed:@"cv.png"];
[images addObject:image3];
UIImage *image4 = [UIImage imageNamed:@"tv.png"];
[images addObject:image4];
if (images.count>0) {
int randomValue = arc4random_uniform(images.count);
img1.image = [images objectAtIndex:randomValue];
img1.tag = randomValue;
int randomValue1 = arc4random_uniform(images.count);
if(randomValue1 == img1.tag)
randomValue1 ++;
if(randomValue1 > [images count] - 1)
randomValue1 = 0;
img2.image = [images objectAtIndex:randomValue1];
img2.tag = randomValue1;
int randomValue2 = arc4random_uniform(images.count);
if (randomValue2 == img1.tag)
randomValue2 ++;
if (randomValue2 == img2.tag)
randomValue2 ++;
if (randomValue2 > [images count] - 1)
randomValue2 = 0;
img3.image = [images objectAtIndex:randomValue2];
img3.tag = randomValue2;
int randomValue3 = arc4random_uniform(images.count);
if (randomValue3 == img1.tag)
randomValue3 ++;
if (randomValue3 == img2.tag)
randomValue3 ++;
if (randomValue3 == img3.tag)
randomValue3 ++;
if (randomValue3 > [images count] - 1)
randomValue3 = 0;
img4.image = [images objectAtIndex:randomValue3];
img4.tag = randomValue3;
有时它会在 2 或 3 个 UIImageView 中给我相同的图像