I am kind of in a pickle here since I have tried many things to accomplish this and have failed.
What I want to do: I have a NSString in my app which is the answer to a question to a round in a quiz. My answers are stored in a NSMutableArray. These answers are chosen at random. I have 4 buttons in my app that are options (possible answers) for the round. I don't want to have multiple answers!
What I have tried:
What I need help with: How should I stop the answer from being in multiple buttons so that the quiz isn't showing the obvious answer?
What should I do instead?
Thanks!
Edit1: So I pretty much made a NSArray of my four UIButtons.
I put the answer into a random UIButton like this:
NSInteger chosen = (arc4random() % 4);
UIButton *randButton = (UIButton *)[buttonArray objectAtIndex:chosen];
[randButton setTitle:imageName forState:UIControlStateNormal];
Then I title the other buttons like so, my buttons that do not have the answer have a title of nothing so I do this:
- (void)titleButtons {
for (UIButton *buttons in buttonArray) {
if ([[buttons titleForState:UIControlStateNormal] == nil]) {
UIButton *button = buttons;
NSString *superheroString = (NSString*)[superheroArray objectAtIndex:(arc4random() % [superheroArray count])];
[button setTitle:superheroString forState:UIControlStateNormal];
[self checkTitles:button];
}
}
Then the checkTitle method looks like this, this is the method where I try to make sure where 2 buttons do not have the same answer as the imageName (answer):
- (void)checkTitles:(UIButton*)button {
if ([[button titleForState:UIControlStateNormal] isEqualToString:imageName]) {
//Duplicate Answer so re-title button
NSString *newTitle = [superheroArray objectAtIndex:(arc4random() % [superheroArray count])];
[button setTitle:newTitle forState:UIControlStateNormal];
//Call same method again to see if new title is still same answer as pic to avoid same answers
[self checkTitles:button];
}
}
}
If you have an NSArray with all your answers, and you want 1 correct answer and 3 different wrong answers, you can do the following: