I can’t shuffle my questions and answers from a plist. Here’s my code - I have 1 label (for the question) and 4 buttons (for the answers), please tell me how to shuffle the plist? I want random questions each time I start the quiz. ![]()
THE .H FILE:
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController {
int mainInt;
IBOutlet UILabel *timer;
NSTimer *randomMain;
//that was for the timer
int currentQuestion;
}
@property (retain, nonatomic) IBOutlet UILabel *timer; //this is for the timer
@property (retain, nonatomic) IBOutlet UILabel *textQuestionTitle;
@property (retain, nonatomic) IBOutlet UIButton *buttonA;
@property (retain, nonatomic) IBOutlet UIButton *buttonB;
@property (retain, nonatomic) IBOutlet UIButton *buttonC;
@property (retain, nonatomic) IBOutlet UIButton *buttonD;
@property (retain, nonatomic) NSArray *questions;
@property (retain, nonatomic) NSString *answer;
-(void)showNextQuestion;
@end
================================================================
THE .M FILE:
#import "SecondViewController.h"
@implementation SecondViewController
@synthesize timer, buttonA, buttonB, buttonC, buttonD, textQuestionTitle;
@synthesize questions = questions_;
@synthesize answer = answer_;
-(void)randomMainVoid {
mainInt -= 1;
timer.text = [NSString stringWithFormat:@"Time left: %d", mainInt];
if (mainInt == 0) {
[randomMain invalidate];
randomMain = nil;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
mainInt = 11;
randomMain = [NSTimer scheduledTimerWithTimeInterval:(1.0/1.0) target:self selector:@selector(randomMainVoid) userInfo:nil repeats:YES];
NSString *path = [[NSBundle mainBundle] pathForResource:@"Questions" ofType:@"plist"];
NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:path];
self.questions = [tempDict objectForKey:@"Root"];
currentQuestion = -1;
[self showNextQuestion];
}
-(void)showNextQuestion {
currentQuestion ++;
if (currentQuestion < [self.questions count]) {
NSDictionary *nextQuestion = [self.questions objectAtIndex:currentQuestion];
self.answer = [nextQuestion objectForKey:@"QuestionAnswer"];
self.textQuestionTitle = [nextQuestion objectForKey:@"QuestionTitle"];
NSString *varA = [nextQuestion objectForKey:@"A"];
[buttonA setTitle: varA forState:UIControlStateNormal];
NSString *varB = [nextQuestion objectForKey:@"B"];
[buttonB setTitle: varB forState:UIControlStateNormal];
NSString *varC = [nextQuestion objectForKey:@"C"];
[buttonC setTitle: varC forState:UIControlStateNormal];
NSString *varD = [nextQuestion objectForKey:@"D"];
[buttonD setTitle: varD forState:UIControlStateNormal];
}
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
Here’s my plist:
http://img526.imageshack.us/img526/875/plistfile.jpg