I have 10 images in array and 10 buttons, all i want is to set the images to buttons randomly.
i tried this but not succeeded
int index = arc4random() % 10;
UIImage *img = [UIImage imageNamed:[arrayAnsewrImages objectAtIndex:index]];
[self.btnAnswerImage1 setBackgroundImage:[UIImage imageNamed:img] forState:UIControlStateNormal];
here is the code of method , m using :
[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 Getting Blank/nil image on my every button. Please suggest me any option.
Nothing strikes me as being that odd in your code, but there are a couple of little things I’d check…
Your call to
[UIImage imageNamed:]needs to take a string as a parameter, yet it appears (at least by the name of your array) that you are passing in a UIImage object. The code should be one of the following:or:
depending on how you want to build your ‘images’ array; with images, or imageNames. Also, I presume the frame of your buttons are set, and that they are added to a view?
Hope you get it sorted.
EDIT
Just looked at your additional code. Just a thought – have you initialised your array
arrayQuestionImages? Make sure you call something similar to:prior to adding the objects.
Also, using a switch statement in the way you have shown above looks a bit obfuscated. Personally, I would do something like the following: