0

I have an NSMutableArray of 4 different objects: Also I have 4 UIImageView's: For obtaining different images from this array i wrote a code but it doesn't work. Any suggestions, please?

images = [[NSMutableArray alloc] init];
    UIImage *image1 = [UIImage imageNamed:@"ca.png"];
    [images addObject:image1];
    UIImage *image2 = [UIImage imageNamed:@"ta.png"];
    [images addObject:image2];
    UIImage *image3 = [UIImage imageNamed:@"cv.png"];
    [images addObject:image3];
    UIImage *image4 = [UIImage imageNamed:@"tv.png"];
    [images addObject:image4];

    if (images.count>0) {
        int randomValue = arc4random_uniform(images.count); 
        img1.image = [images objectAtIndex:randomValue];
        img1.tag = randomValue;

        int randomValue1 = arc4random_uniform(images.count);

        if(randomValue1 == img1.tag)
            randomValue1 ++;

        if(randomValue1 > [images count] - 1)
            randomValue1 = 0;

        img2.image = [images objectAtIndex:randomValue1];
        img2.tag = randomValue1;

        int randomValue2 = arc4random_uniform(images.count);

        if (randomValue2 == img1.tag) 
            randomValue2 ++;

        if (randomValue2 == img2.tag)
            randomValue2 ++;

        if (randomValue2 > [images count] - 1)
            randomValue2 = 0;

        img3.image = [images objectAtIndex:randomValue2];
        img3.tag = randomValue2;

        int randomValue3 = arc4random_uniform(images.count);

        if (randomValue3 == img1.tag)
            randomValue3 ++;

        if (randomValue3 == img2.tag)
            randomValue3 ++;

        if (randomValue3 == img3.tag)
            randomValue3 ++;

        if (randomValue3 > [images count] - 1)
            randomValue3 = 0;

        img4.image = [images objectAtIndex:randomValue3];
        img4.tag = randomValue3;

Sometimes it gives me the same image in 2 or 3 UIImageView's

3 Answers3

0

First you randomize your mutable array. See this question to find out how. Then just assign your images easy way:

[images shuffle];

img1.image = [images objectAtIndex:0];
img2.image = [images objectAtIndex:1];
img3.image = [images objectAtIndex:2];
img4.image = [images objectAtIndex:3];
Community
  • 1
  • 1
Adam
  • 26,549
  • 8
  • 62
  • 79
0

Now I made it in another way

- (IBAction)ready:(id)sender {

    [ready setHidden:YES];
    [impimage setHidden:NO];
    [img1 setUserInteractionEnabled:YES];
    [img2 setUserInteractionEnabled:YES];
    [img3 setUserInteractionEnabled:YES];
    [img4 setUserInteractionEnabled:YES];


    if (images.count > 0) {
        int randomValue = arc4random_uniform(images.count); 
        impimage.image = [images objectAtIndex:randomValue];

        [img1 setImage:[UIImage imageNamed:@""]];
        [img2 setImage:[UIImage imageNamed:@""]];
        [img3 setImage:[UIImage imageNamed:@""]];
        [img4 setImage:[UIImage imageNamed:@""]];        
    }

}

- (void) onImageClick:(UITapGestureRecognizer*)sender
{   

    UIImageView *m_current = (UIImageView*)sender.view;


    if( [images1 objectAtIndex:m_current.tag] == impimage.image)
    {
        [m_current setImage:[images1 objectAtIndex:m_current.tag]];

        int randomValue = arc4random_uniform(images1.count); 

        if([images1 count])
        {
            if(randomValue == m_current.tag)
                randomValue++;

            if(randomValue > [images1 count] - 1)
                randomValue = 0;

            [impimage setImage:[images1 objectAtIndex:randomValue]];

        }

        NSLog(@"right");
    }

    else NSLog(@"wrong");

    if(img2.image !=nil && img1.image!=nil && img3.image !=nil && img4.image !=nil)
    {
        UIAlertView *alert  = [[UIAlertView alloc] initWithTitle:nil 
                                                         message:@"level completed" 
                                                        delegate:nil cancelButtonTitle:@"OK" 
                                               otherButtonTitles:nil];
        [alert show];

        _level5 = [[Level5 alloc] init];
        [self.view addSubview:_level5.view];
    }

}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [impimage setHidden:YES];
    [img1 setUserInteractionEnabled:NO];
    [img2 setUserInteractionEnabled:NO];
    [img3 setUserInteractionEnabled:NO];
    [img4 setUserInteractionEnabled:NO];

    images = [[NSMutableArray alloc] init];
    UIImage *image1 = [UIImage imageNamed:@"ca.png"];
    [images addObject:image1];
    UIImage *image2 = [UIImage imageNamed:@"ta.png"];
    [images addObject:image2];
    UIImage *image3 = [UIImage imageNamed:@"cv.png"];
    [images addObject:image3];
    UIImage *image4 = [UIImage imageNamed:@"tv.png"];
    [images addObject:image4];

    if (images.count>0) {
        int randomValue = arc4random_uniform(images.count); 
        img1.image = [images objectAtIndex:randomValue];
        img1.tag = randomValue;

        int randomValue1 = arc4random_uniform(images.count);

        if(randomValue1 == img1.tag)
            randomValue1 ++;

        if(randomValue1 > [images count] - 3)
            randomValue1 = 0;

        [images removeObjectAtIndex:img1.tag];

        img2.image = [images objectAtIndex:randomValue1];
        img2.tag = randomValue1;

        int randomValue2 = arc4random_uniform(images.count);

        if (randomValue2 == img2.tag) 
            randomValue2 ++;

        if (randomValue2 > [images count] - 2)
            randomValue2 = 0;

        [images removeObjectAtIndex:img2.tag];

        img3.image = [images objectAtIndex:randomValue2];
        img3.tag = randomValue2;

        int randomValue3 = arc4random_uniform(images.count);

        if (randomValue3 == img3.tag)
            randomValue3 ++;

        if (randomValue3 > [images count] - 1)
            randomValue3 = 0;

        img4.image = [images objectAtIndex:randomValue3];
        img4.tag = randomValue3;

        images1 = [[NSMutableArray alloc] init];
        UIImage *image1 = img1.image;
        [images1 addObject:image1];
        UIImage *image2 = img2.image;
        [images1 addObject:image2];
        UIImage *image3 = img3.image;
        [images1 addObject:image3];
        UIImage *image4 = img4.image;
        [images1 addObject:image4];


    }

    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onImageClick:)];
    UITapGestureRecognizer* tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onImageClick:)];
    UITapGestureRecognizer* tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onImageClick:)];
    UITapGestureRecognizer* tap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onImageClick:)];
    [img1 addGestureRecognizer:tap];
    [img2 addGestureRecognizer:tap1];
    [img3 addGestureRecognizer:tap2];
    [img4 addGestureRecognizer:tap3];
}
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
  • 2
    Sergiu, this section is intended for answers. I've removed those parts of your answer that were actually further questions. Please feel free to post a new question if you like. – Michael Petrotta Aug 31 '12 at 05:12
0

When you set first image

        img1.image = [images objectAtIndex:randomValue];

After this you need

        [images removeObjectAtIndex:randomValue];

But you are only decrease count of images array.

 [images count] - 1

It's not Right.

Note if you don't want remove object from original array. you should make a mutable copy of images array.

Hardeep Singh
  • 942
  • 8
  • 15