I have created a quiz which draws questions from a plist into an array and displays the question in a uilabel and the three possible answers in uibuttons. My question is how can I create a random order for the buttons so that the three answers don’t always show up in the same order?
Any help would be most appreciated.
Jamie
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:
and put this function somewhere:
This basically assigns a random number (between 0 and 100) to each question and sorts it by that number. You can then just make
sortedArrayglobal (or possibly just overwrite yourquestionsarray) 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
currentQuestionsArrayand assign it to[questions objectAtIndex:r]before the code above)and
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!