I probably deserve to get shot by good programmers and great minds alike right now because of how my current code looks like even if I’m a total noob at Objective-C. Anyway, I’m making a logo quiz wherein the questions are in random order, then for each question the choices are also in random order. Anyway, I already have this code below to check where the page should redirect when an answer is selected:
- (IBAction)answerA:(UIButton *)sender {
if(imgView.image == [UIImage imageNamed:@"1.png"])
{
if([btnA.titleLabel.text isEqualToString:@"1"])
{
[btnA addTarget:self action:@selector(correctView) forControlEvents:UIControlEventTouchUpInside];
}
else
{
[btnA addTarget:self action:@selector(wrongView) forControlEvents:UIControlEventTouchUpInside];
}
}
else if(imgView.image == [UIImage imageNamed:@"2.png"])
{
if([btnA.titleLabel.text isEqualToString:@"2"])
{
[btnA addTarget:self action:@selector(correctView) forControlEvents:UIControlEventTouchUpInside];
}
else
{
[btnA addTarget:self action:@selector(wrongView) forControlEvents:UIControlEventTouchUpInside];
}
}
- (IBAction)answerB:(UIButton *)sender {
if(imgView.image == [UIImage imageNamed:@"1.png"])
{
if([btnB.titleLabel.text isEqualToString:@"1"])
{
[btnB addTarget:self action:@selector(correctView) forControlEvents:UIControlEventTouchUpInside];
}
else
{
[btnB addTarget:self action:@selector(wrongView) forControlEvents:UIControlEventTouchUpInside];
}
}
else if(imgView.image == [UIImage imageNamed:@"2.png"])
{
if([btnB.titleLabel.text isEqualToString:@"2"])
{
[btnB addTarget:self action:@selector(correctView) forControlEvents:UIControlEventTouchUpInside];
}
else
{
[btnB addTarget:self action:@selector(wrongView) forControlEvents:UIControlEventTouchUpInside];
}
}
}
This works but the problem is, you have to click on the button twice for it to go to the corresponding page. I know that the problem is IBAction itself is already an event that triggers once the button is clicked, then I have the code inside the condition statements that would be only triggered if the button is clicked again. My question is, how can I modify this so that if the button is clicked once, it immediately checks for the current question then the correct answer?
Use following Code. I am writing one method only, you can write second.