我有10个图像阵列和10个按钮,我想要的是随机设置图像到按钮。
我试过这个但没有成功
int index = arc4random() % 10;
UIImage *img = [UIImage imageNamed:[arrayAnsewrImages objectAtIndex:index]];
[self.btnAnswerImage1 setBackgroundImage:[UIImage imageNamed:img] forState:UIControlStateNormal];
这是方法的代码,m使用:
[self.arrayQuestionImages addObject:@"bear.jpg"];
[self.arrayQuestionImages addObject:@"girraf.jpg"];
[self.arrayQuestionImages addObject:@"hippo.jpg"];
[self.arrayQuestionImages addObject:@"monkey.jpg"];
[self.arrayQuestionImages addObject:@"piglet.jpg"];
[self.arrayQuestionImages addObject:@"pumba.jpg"];
[self.arrayQuestionImages addObject:@"rabbit.jpg"];
[self.arrayQuestionImages addObject:@"simba.jpg"];
[self.arrayQuestionImages addObject:@"snake.jpg"];
[self.arrayQuestionImages addObject:@"tigger.jpg"];
[self.arrayQuestionImages addObject:@"turtle.jpg"];
int index = arc4random() % 10;
UIImage *img = [UIImage imageNamed:[arrayQuestionImages objectAtIndex:index]];
switch (index)
{
case 0:
{
//[self.btnQuestionImage1 setBackgroundImage:[UIImage imageNamed:img] forState:UIControlStateNormal];
[self.btnAnswerImage1 setImage:img forState:UIControlStateNormal];
}
break;
case 1:
{
[self.btnQuestionImage2 setImage:img forState:UIControlStateNormal];
}
break;
case 2:
{
[self.btnQuestionImage3 setImage:img forState:UIControlStateNormal];
}
break;
case 3:
{
[self.btnQuestionImage4 setImage:img forState:UIControlStateNormal];
}
break;
case 4:
{
[self.btnQuestionImage5 setImage:img forState:UIControlStateNormal];
}
break;
case 5:
{
[self.btnQuestionImage6 setImage:img forState:UIControlStateNormal];
}
break;
case 6:
{
[self.btnQuestionImage7 setImage:img forState:UIControlStateNormal];
}
break;
case 7:
{
[self.btnQuestionImage8 setImage:img forState:UIControlStateNormal];
}
break;
case 8:
{
[self.btnQuestionImage9 setImage:img forState:UIControlStateNormal];
}
break;
case 9:
{
[self.btnQuestionImage10 setImage:img forState:UIControlStateNormal];
}
break;
default:
{
[self.btnQuestionImage1 setImage:img forState:UIControlStateNormal];
}
break;
}
}
M在我的每个按钮上获取空白/零图像。请建议我任何选择。
答案 0 :(得分:0)
UIImage * img = [UIImage imageNamed:[arrayAnswerImageNames objectAtIndex:index]]; [self.btnAnswerImage1 setBackgroundImage:img forState:UIControlStateNormal];
答案 1 :(得分:0)
您需要将图像名称放在数组中。
您正在尝试将uiimage对象传递给imagedNamed构造函数
答案 2 :(得分:0)
在你的代码中 奇怪,没有什么事情让我感到震惊,但是我会检查一些小事......
您对[UIImage imageNamed:]
的调用需要将字符串作为参数,但出现(至少通过数组的名称),您将在UIImage对象中传递。代码应该是以下之一:
UIImage *img = [UIImage imageNamed:[arrayAnswerImageNames objectAtIndex:index]]; // Note "Names" array
或:
UIImage *img = (UIImage *)[arrayAnswerImages objectAtIndex:index];
取决于您想要如何构建“图像”数组;带有图像或图像名称。另外,我假设按钮的框架已设置,并且它们被添加到视图中?
希望你把它分类。
的修改
只是查看了您的其他代码。只是一个想法 - 你初始化了你的数组arrayQuestionImages
?确保你打电话给类似的东西:
self.arrayQuestionImages = [NSMutableArray arrayWithCapacity:10];
在添加对象之前。
此外,以上面显示的方式使用switch语句看起来有点混淆。就个人而言,我会做以下事情:
- (void)viewDidLoad
{
[super viewDidLoad];
self.questionImageNames = [NSArray arrayWithObjects:@"bear.jpg", @"girraf.jpg", @"hippo.jpg", @"monkey.jpg", @"piglet.jpg", @"pumba.jpg", @"rabbit.jpg", @"simba.jpg", @"snake.jpg", @"tigger.jpg", @"turtle.jpg", nil];
NSMutableArray *buttonArray = [NSMutableArray arrayWithCapacity:10];
for (int i = 0; i < NUM_BUTTONS; i++) {
NSUInteger index = arc4random() % NUM_BUTTONS;
UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom];
newButton.frame = CGRectMake(i * x, i * y, width, height); // Whatever position and size you need...
UIImage *buttonImage = [UIImage imageNamed:[self.questionImageNames objectAtIndex:index]];
[newButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:newButton];
[buttonArray addObject:newButton];
}
self.myButtons = buttonArray; // Where myButtons is a NSArray property of your class, to store references to the buttons.
}