0
votes

I am writing a simple application that consists of 2 UIImageViews and 2 UIButtons. The UIImageViews are placed on top of the UIButtons.

I have 1 UIImage that appears on a random UIImageView and disappears after 2 seconds. The user then has to tap on the button they think the image appeared on.

I shuffle the UIImageView array and use the first element (index 0) for the image to be displayed on. When shuffling I keep track of which element (i.e from which index. lets say from index "n") was placed on index 0 of the array.

However, when a button is pressed i compare the id of the button pressed with the id of the button with index n. (because that is the index of the random UIImageView). But for some reason it is not working. :(

Here is my code: (i didn't upload the .h file. it just contains declarations.) The code below doesn't produce any error/warning messages. It just doesn't output the result I want.

@interface ViewController ()
@property (strong, nonatomic) NSMutableArray* tempButton;
@property (strong, nonatomic) NSMutableArray *tempViews;
@property (strong, nonatomic) UIImage *myImage;
@end

@implementation ViewController



- (void)viewDidLoad
{
    [super viewDidLoad];

     _myImage = [UIImage imageNamed:@"hello"];
    _tempButton = [[NSMutableArray alloc] initWithObjects:_button1, _button2, nil];

    _tempViews = [[NSMutableArray alloc]initWithObjects:_image1, _image2, nil];


     [self displayonView];

}



-(void)displayToFindSymbol{
    [_toFindImage setImage:_myImage];
    _toFindImage.contentMode = UIViewContentModeScaleAspectFit;
}

- (IBAction)pressed:(id)sender {

    UIButton *result = (UIButton *)sender;
    if (result == _tempButton[_correctButton]) {
       NSLog (@"Correct");
    }
    else if (result != _tempButton[_correctButton]){
       NSLog (@"Incorrect");            

    }

}



-(NSMutableArray *)shuffleViews{
    NSUInteger count = _tempViews.count;
    int n;
    for (int i=count-1; i>=0; --i) {

        n = arc4random()  % (i + 1);


        [_tempViews exchangeObjectAtIndex:n withObjectAtIndex:i];
    }
    _correctButton = n;
    return _tempViews;

}

-(void)displayonView{

    NSMutableArray *tempArray;
    tempArray = [self shuffleViews]; // shuffle views

     _correctImage = [tempArray objectAtIndex:0];

    _correctImage.contentMode = UIViewContentModeScaleAspectFit;

    [_correctImage setImage:_myImage];

    NSTimer* myTImer;
    myTImer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(hideLabel) userInfo:nil repeats:NO];

}

-(void) hideLabel{
    _correctImage.hidden = YES;
    for (UIButton *each in self.tempButton) {
        each.enabled = YES;
    }
    [self displayToFindSymbol];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
2

2 Answers

0
votes

You could either have each button call a different method as the selector, or assign a different tag to each button and inspect it in a shared method.

0
votes

Add all imageviews in an array and also all buttons in an array.

when setting image to random image view store its index and use the index later on to get the corresponding button that should have been clicked.

Pseudo code

NSArray a = [NSArray arrayWithObjects:imgVie1,ImgView2...,nil]
// create second array with buttons 

int randomIndex = arc4random() % a.count;
UIImageView *imgView = [a objectAtIndex:randomIndex];
//set image and hide later

UIButton *correctButton = [secondarray objectAtIndex:randomIndex];