我对Objective-C和编程很新。
以下代码是我从各个帖子拼凑而来的。
模拟器构建和启动(但按钮1上没有图像)。
尝试将随机图像(从5张图像)放到UIButton
。
.m文件中的代码:
@property (nonatomic, strong) NSMutableArray* imageArray;
- (void)viewDidLoad
{
[super viewDidLoad];
_imageArray =[[NSMutableArray alloc] init];
[UIImage imageNamed:@"btn1.png"],
[UIImage imageNamed:@"btn2.png"],
[UIImage imageNamed:@"btn3.png"],
[UIImage imageNamed:@"btn4.png"],
[UIImage imageNamed:@"btn5.png"];
int index = arc4random() % 5;
for (int i = 0; i < [_imageArray count]; i++)
[_button1 setImage:[UIImage imageNamed:[_imageArray objectAtIndex:index]]
forState:UIControlStateNormal];
}
任何正确方向的帮助都会很棒。
答案 0 :(得分:0)
试试这个:
- (void)viewDidLoad {
[super viewDidLoad];
_imageArray =[[NSMutableArray alloc] init];
[_imageArray addObject:[UIImage imageNamed:@"btn1.png"]] ;
[_imageArray addObject:[UIImage imageNamed:@"btn2.png"]] ;
[_imageArray addObject:[UIImage imageNamed:@"btn3.png"]] ;
[_imageArray addObject:[UIImage imageNamed:@"btn4.png"]] ;
[_imageArray addObject:[UIImage imageNamed:@"btn5.png"]] ;
int index = arc4random() % 5;
[_button1 setImage:[UIImage imageNamed:[_imageArray objectAtIndex:index]] forState:UIControlStateNormal];
}