Reputation: 2221
I am new to Objective-C and X-Code and basically programming in iOS so this may be a very simple question. Anyway, I'm trying to make a logo quiz wherein the logos and the choices are generated in random order i.e. (Logo 1) --- Choices: A B C D for the first try. Then the next time the user opens the app, Logo 4 appears first with choices A D B C. (I hope that's clear). I've managed to randomly display the logos but can't figure out how to do the same for the choices which would modify the value of 4 buttons below the logo.
From what I've search, you use something like:
[btnA setTitle:@"answer" forState:UIControlStateNormal];
My problem is, I'm thinking of putting the choices into an array as well so I tried something such as this:
[btnA setTitle:[answerArray objectAtIndex:i] forState:UIControlStateNormal];]
Similar to what I've done for the logos but I'm not getting any values for the button. Is there something I'm missing here? I don't get any errors either. Here's a part of my code that might better explain what I've been trying to do:
NSMutableArray *answerList = [[NSMutableArray alloc] initWithObjects:
@"AnswerA",
@"AnswerB",
@"AnswerC",
nil];
answerArray = answerList;
int i = arc4random() % [answerArray count];
[self.btn1 setTitle:[answerArray objectAtIndex:i] forState:UIControlStateNormal];
[self.btn2 setTitle:[answerArray objectAtIndex:i] forState:UIControlStateNormal];
[self.btn3 setTitle:[answerArray objectAtIndex:i] forState:UIControlStateNormal];
Upvotes: 0
Views: 2237
Reputation: 38249
Just Shuffle
your buttonTitleArray
which is MutableArray
without repeating any same logo quiz
using this link.
Also Shuffle logo quiz's choice
to make it interactive with above link
.
Upvotes: 1
Reputation: 6969
See following code - it can generate random title from set of 100:
-(int)getRandomNumber:(int)fromVal to:(int)toVal {
return (int)fromVal + arc4random() % (toVal-fromVal+1);
}
-(void) setButtonTitle
{
NSArray * buttonTitleArray = [NSArray arrayWithObjects:@"title1", @"title2",... @"title100", nil};
int x = [getRandomNumber :0 :100];
NSString * title = [buttonTitleArray objectAtIndex:x];
[button setTitle:title :forState:UIControlStateNormal];
}
Upvotes: 1