Objective C : UIButton image with NSArray in a random order

Posted by Sarah on Stack Overflow See other posts from Stack Overflow or by Sarah
Published on 2012-03-21T04:36:11Z Indexed on 2012/03/21 5:30 UTC
Read the original article Hit count: 135

Brief Idea about the flow :

I have say minimum 1 and maximum 18 records in my database with a text and an image from which I have to fetch images and place any 3 of the same randomly to 3 UIButtons among which one image should match with my question. i.e. 1 is the answer and other 2 are the distractors.

For Example : Question is : where is Apple? Now among the bunch of images, I want to place any 3 images on the 3 UIButtons(1 answer i.e. An Apple and 2 distractors) in a random order and on selecting the UIButton it should prompt me if it's a right answer or not.

I am implementing below code for placing the UIImages in a random order :

-(void) placeImages 
{
    NSMutableArray *images = [NSMutableArray arrayWithArray:arrImg];

    NSArray *buttons = [NSArray arrayWithObjects:btn1,btn2,btn3, nil];
    for (UIButton *btn in buttons)
    {
        int randomIndex= random() % images.count;
        UIImage *img = [images objectAtIndex:randomIndex];
        [btn setImage:img forState:UIControlStateNormal];
        [images removeObjectAtIndex:randomIndex];                        
    }
}

But I am stuck at a point that how should I get 1 UIButton image with an answer and other as distractors also that how should i maintain the index of the image from the NSArray? Kindly guide me. Thank you.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about sqlite3