インターフェイスに表示される質問と回答 (指定された質問を含む) でいっぱいの plist がありますが、質問をランダムに表示したいと考えています。以下で試したコードを参照してください。
- (void)viewDidLoad
{
[super viewDidLoad];
if (questions && configDictionary) {
for (int i = 0; i < [questions count]; ++i) { int r = (random() % [questions count]); [questions exchangeObjectAtIndex:i withObjectAtIndex:r]; }
[questionLabel setText:[[questions objectAtIndex:currentQuestonIndex] objectForKey:@"question"]];
NSArray *answers = [[questions objectAtIndex:currentQuestonIndex] objectForKey:@"answers"];
[answerLabel0 setText:[answers objectAtIndex:0]];
[answerLabel1 setText:[answers objectAtIndex:1]];
[answerLabel2 setText:[answers objectAtIndex:2]];
[answerLabel3 setText:[answers objectAtIndex:3]];
[pointsPerAnswerLabel setText:[NSString stringWithFormat:@"+%d points", [[configDictionary objectForKey:kPointsPerCorrectAnswer] intValue]]];
[currentQuestionNumberLabel setText:[NSString stringWithFormat:@"question %d", currentQuestonIndex+1]];
}
}
Someone please help?
「NSArray」は「exchangeObjectAtIndex:withObjectAtIndex:」に応答しない可能性があるという警告が表示され、実行しようとするとクラッシュします。
以下の .h ファイルを参照してください。
@interface ViewController : UIViewController {
NSDictionary *configDictionary;
NSMutableArray *questions;
int currentQuestonIndex;
NSMutableArray *questionsCorrectlyAnswered;
NSTimer *timer;
int totalTimeLeft;
int currentTimeLeft;
BOOL saveTime;
}
@property (retain, nonatomic) NSMutableArray *questions;
それはまだクラッシュします。
ビューの前の私のテーブルビュー:
- (void)tableView:(UITableView *)_tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
gameViewController = [[QuizViewController alloc] initWithNibName:@"QuizViewController"bundle:nil];
[(QuizViewController*) gameViewController setMasterViewController:self];
[(QuizViewController*) gameViewController setTitle:[[quizzes objectAtIndex:indexPath.row] objectForKey:@"quizName"]];
[(QuizViewController*) gameViewController setQuestions:[[quizzes objectAtIndex:indexPath.row] objectForKey:@"questions"]];
[gameViewController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentModalViewController:gameViewController animated:YES];
}