2
votes

I'm new to Objetive-C and need some help,

I have an Array with 22 UIImage's and 4 UIButtons on the view. I want to take an image from the array with ObjectAtIndex:somecounter and place this image randomly in one of the 4 UIButton's, than I want to take another 3 UIImages from the Array (random images) and locate them randomly in the other 3 UIButtons that I have left.

so in each time that the counter is in a number, take the UIImage that stored in this index, locate it randomly in one of the UIButtons, than take other 3 random UIImages from the Array and locate them randomly on the 3 UIButton that I have left.

how can I do that?

after using basvk answer came up with this:

-(void)PlaceWordAndPictueOnScreen
{
    NSArray *buttons = [NSArray arrayWithObjects:btnLetter1, btnLetter2, btnLetter3, btnLetter4, nil];
    for (UIButton *btn in buttons) {
        int randomIndex= random() % [LettersArray count];
        UIImage *img = [LettersArray objectAtIndex:randomIndex];
        [btn setImage:img forState:UIControlStateNormal];
        [LettersArray removeObjectAtIndex:randomIndex];                        
    }
}

so I did exactly as he said and it worked like charm, but, I do have a problem. I have this counter called 'imgcounter', lets say he's value is '4', I want to take the image that located at index 4 and put it in one of the 'btnLetter' UIButton, but in a random button, than in the other 3 'btnLetter' buttons that left, to put another 3 random UIImage's, how can I do that?

Thanks..

3
Hi Yossi. It would be helpful if you can show us how you have approached the problem already. Show us any code you have written and we can point you in the right direction. - Ashley Mills
Just rewrite the code so it picks a random() button from a NSMutableArray filled with your buttons. Don't forget to remove that button from the array, so it won't be picked later on. - basvk

3 Answers

1
votes

Out the top of my head, something like this:

  -(void)placeImages {
        NSMutableArray *images = [NSMutableArray arrayWithObjects:@"image1.png", @"image2.png", @"image3.png", @"image4.png", @"image5.png", @"image6.png", @"image7.png", @"image8.png", nil]; // etc...
        NSArray *buttons = [NSArray arrayWithObjects:btn1, btn2, btn3, btn4, nil];
        for (UIButton *btn in buttons) {
              int randomIndex= random() % images.length;
              UIImage *img = [images objectAtIndex:randomIndex];
              [btn setImage:img forState:UIControlStateNormal];
              [images removeObjectAtIndex:randomIndex];                        
        }
0
votes
-(void)randomizeArray:(NSMutableArray *)array
{
    int i, n = [array count];
    for(i = 0; i < n; i++) {
        int destinationIndex = random() % (n - i) + i;
        [array exchangeObjectAtIndex:i withObjectAtIndex:destinationIndex];
    }
}

this is a answer for your question

-1
votes

btnLetter1, btnLetter2, btnLetter3, btnLetter4 = UIButtons that should present an image.

LatterArray = the array that containes all of the images.

imgcounter = the current counter index

if the 'imgcounter' is on '2' so one of the 'btnLetters' buttons (random one between 1-4) should containe an image from 'LettersArray' that located in index number '2' equal to the 'imgcounter'. all the other remining 3 'btnLetter' should containe a random image from 'LettersArray'.

when the function called again the 'imgcounter' will be '3', now one of the 'btnLetters' (random one) should containe another image from 'LettersArray that is located at index number '3', again, equal to the 'imgcounter'. the other remining 3 'btnLetter' should containe again randome image from 'Letters Array' and so on..

-(void)PlaceWordAndPictueOnScreen2
{
    NSMutableArray * ButtonArray = [[NSMutableArray alloc] initWithObjects:btnLetter1,btnLetter2,btnLetter3,btnLetter4, nil];
    int CorrectImg = random() % [ButtonArray count];

    UIImage * img = [UIImage imageNamed:[LettersArray objectAtIndex:imgcounter]];
    UIButton * btn = [ButtonArray objectAtIndex:CorrectImg];
    [btn setImage:img forState:UIControlStateNormal];
    [ButtonArray removeObjectAtIndex:CorrectImg];

    NSLog(@"img correct: %i",CorrectImg);

    while ([ButtonArray count] != 0)// how many times u want to run this
    {
        int imgRand = random() % [LettersArray count]; //number for random image
        int btnRand = random() % [ButtonArray count]; //number for random button
        //get that random image
        UIImage * img = [UIImage imageNamed:[LettersArray objectAtIndex:imgRand]];
        //get that random button
        UIButton * button = [ButtonArray objectAtIndex:btnRand];
        //put image on that button
        [button setImage:img forState:UIControlStateNormal];
        [ButtonArray removeObjectAtIndex:btnRand];

        NSLog(@"btnrnd: %i",btnRand);
        NSLog(@"imgrnd: %i",imgRand);
    }
}