I need your help as I dont know how can I set the background image of my UIButtons from array of images. I have 10 UIButtons and an array of 10 images. I want to set the images from array randomly. Please tel me how this can be possible.
Asked
Active
Viewed 487 times
2 Answers
0
This may need tweaking a bit, I haven't tested it, but the concept should be ok.
NSArray *arr = [NSArray arrayWithObjects:@"1.png", @"2.png", ...., nil];
int index = arc4random() % 10;
UIImage *img = [UIImage imageNamed:[arr objectAtIndex:index]];

James Webster
- 31,873
- 11
- 70
- 114
0
1)First declare a NSMutableArray in which you will store the images :
NSMutableArray *yourImgArray;
2)Allocate the array in .m file :
yourImgArray=[[NSMutableArray alloc]initWithObjects:@"1.jpg",@"2.jpg",@"3.jpg",nil
];
3)Set the images while setting the tags(in your loop):
[yourButtonInstance setBackgroundImage:yourImageInstance forState:UIControlStateNormal];
Hope it helps !!

Akshay
- 2,973
- 6
- 43
- 75