ios - 如何在 ViewController 中随机化 3 张图像?

标签 ios xcode storyboard imageview

在我的应用程序中,我有一个带有 ImageView 的主视图 Controller 。每次用户返回此主视图时,我希望出现 3 张图像中的 1 张,但我想将它们随机化。有谁知道完成这项工作的最佳方法?有人能指出我正确的方向吗?

这是一个带有 Storyboard的 xcode 中的通用 ios 应用程序。

谢谢!

最佳答案

像下面这样的东西应该可以解决问题......

NSArray *imageArray = @[@"ImageName1", @"ImageName2", @"ImageName3"];
NSUInteger randomImageIndex = arc4random() % [imageArray count];
[yourImage setImage:[UIImage imageNamed:[imageArray objectAtIndex:randomImageIndex]]];

编辑:

将以下内容添加到您的 View Controller .h 文件中。

@property (strong, nonatomic) IBOutlet UIImageView *yourImage;

然后在您的 Storyboard/ View 中将 UIImageView 链接到您的图像。

关于ios - 如何在 ViewController 中随机化 3 张图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12696657/

相关文章:

ios - UICollectionView 状态恢复 : restore all UICollectionViewCells

ios - 检索函数数据

ios - 层次结构顶层的初始 View Controller 丢失 NavigationBar

ios - 如何在 objective-c 中的头文件中访问定义的常量数组?

ios - 在 ios 的 uitableview 中自定义单元格编辑样式

objective-c - 应用程序在渲染期间进入后台时 AVAssetWriter 失败

ios - UITableViewCell 自动高度基于 UILabel 文本的数量

ios - Swift edgeLoopF​​orRect 物理主体未正确获取 categoryBitMask

ios - 如何判断模拟器中运行的设备?

objective-c - 如何从 Storyboard 对 Nib 进行转场