逻辑:
- 数组中的 5 个图像
例子:
apple.png,mango.png,bird.png,sun.png,moon.png
我需要在这个数组中使用arc4random
,所以我得到索引为1,所以值得到mango.png
在我存储的其他数组中
number = [[NSMutableArray alloc]initWithObjects:@"4",@"2",@"9",@"1",@"8",@"7",@"5",@"3",@"6",@"10", nil];
并使用了 arc4random
,我得到的输出为 index:2 value as:9
然后我需要在 ImageView 中打印 9 次芒果图像。
请帮助如何使用 objective-c 对此进行编码
最佳答案
根据我的理解,你必须根据随机数在 ImageView
中显示随机图像。
看下面的代码,
images = [[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"Navarre-Family-Eye-Care-Navarre-Florida-Optometrist-Santa-Christmas-Toy-Safety.jpg"],[UIImage imageNamed:@"Christmas-Wallpapers-HD-Picture.jpg"],[UIImage imageNamed:@"Christmas-Wallpaper-jesus-9413550-1024-768.jpg"],[UIImage imageNamed:@"tree.jpg"],[UIImage imageNamed:@"luxury-christmas-napkins-father-christmas-1635-p.jpg"],[UIImage imageNamed:@"Navarre-Family-Eye-Care-Navarre-Florida-Optometrist-Santa-Christmas-Toy-Safety.jpg"],[UIImage imageNamed:@"Christmas-Wallpapers-HD-Picture.jpg"],[UIImage imageNamed:@"Christmas-Wallpaper-jesus-9413550-1024-768.jpg"],[UIImage imageNamed:@"tree.jpg"],[UIImage imageNamed:@"luxury-christmas-napkins-father-christmas-1635-p.jpg"], nil];
int randomIndex=arc4random() % images.count;
UIImage *selectedImage = [images objectAtIndex:randomIndex]; //random selected image
number = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10", nil];
int randomNumber = arc4random() % number.count; //random selected number
NSInteger indexValue = [number indexOfObject:[NSString stringWithFormat:@"%d",randomNumber]]; // get index number of your random number
for (int i = 0; i <indexValue ; i++) {
NSLog(@"%@", selectedImage);
yourImageView.Image = selectedImage;
}
关于ios - 如何在 objective-c 中使用 for 循环显示存储在数组中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45994021/