Going along with @Abizern's answer. It would be more efficient to have the fixed buttons and randomise the array. You could do something like this:
//NSMutableArray *questions = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", @"4", nil];
NSMutableArray *unorderedQuestions = [[NSMutableArray alloc] init];
for (int i=0; i < [questions count]; i++) {
int lowerBound = 0;
int upperBound = 100;
int rndValue = lowerBound + arc4random() % (upperBound - lowerBound);
[unorderedQuestions addObject:[[NSArray alloc] initWithObjects:[questions objectAtIndex:i], [NSString stringWithFormat:@"%i", rndValue], nil]];
}
NSArray* sortedArray = [unorderedQuestions sortedArrayUsingFunction:order context:NULL];
//or
// questions = [unorderedQuestions sortedArrayUsingFunction:order context:NULL];
NSLog(@"%@", sortedArray);
and put this function somewhere:
static NSInteger order (id a, id b, void* context) {
NSString* catA = [a lastObject];
NSString* catB = [b lastObject];
return [catA caseInsensitiveCompare:catB];
}
This basically assigns a random number (between 0 and 100) to each question and sorts it by that number. You can then just make sortedArray
global (or possibly just overwrite your questions
array) and display objects sequentially and they will be shuffled.
[btnA setTitle:[[[questions objectAtIndex:r] objectAtIndex:0] objectForKey:@"A"] forState:UIControlStateNormal];
Judging by your code, you may need to amend a few lines: (but you could also create a currentQuestionsArray
and assign it to [questions objectAtIndex:r]
before the code above)
for (int i=0; i < [questions count]; i++) {
//to
for (int i=0; i < [[questions objectAtIndex:r] count]; i++) {`
and
[unorderedQuestions addObject:[[NSArray alloc] initWithObjects:[questions objectAtIndex:i], [NSString stringWithFormat:@"%i", rndValue], nil]];
//to
[unorderedQuestions addObject:[[NSArray alloc] initWithObjects:[[questions objectAtIndex:r] objectAtIndex:i], [NSString stringWithFormat:@"%i", rndValue], nil]];`
I think this is the most robust solution, as in the future you may have a different number of answers for a particular question and would not need to edit this to do so!