Ok, now that we've go the correct question being asked, our next task is correct the logic which checks if the correct answer has been chosen. This currently exists in an "answer" view, which will later be folded into the question view. But for now, let's go ahead and get that piece working. It shouldn't take much time.
Here's the relevant code:
NSUInteger selection = [index row];
// increment to for comparison with correct anser
selection++;
// get the app state
appState = delegate.appState;
// get the current question
Question * currentQuestion = [appState currentQuestion];
questionsAsked.text = [NSString stringWithFormat:@"%d", currentQuestion.number];
remaining.text = [NSString stringWithFormat:@"%d", appState.numberOfQuestions -
currentQuestion.number];
if (selection == currentQuestion.correctAnswer) {
resultView.text = @"Correct!";
appState.correctAnswers++;
[appState saveState];
}
else
{
// Get the correct answer
NSString *lit = @"Incorrect, the answer is: ";
NSString *answer = [currentQuestion correctAnswerText];
NSString *myAnswer = [lit stringByAppendingString:answer];
resultView.text = myAnswer;
}
So, it's really comparing the row selected to the correct number. What we want to do is extract the text stored in the row selected, and compare it with the hiragana field of the current question.
So, how to do that? We could figure out how to access the table - or just use the data structure that was used to initialize int in the first place, the "answers" array in the QuestionController. Do we currently have access to it?
Before I forget, this looks like a good way to finally center align the "question", which is currently left-shifted:
pctCorrect.textAlignment = UITextAlignmentRight;
Unfortunately, we don't have that array in the AnswerController. Anyway way we can simply pass the "correct" index?
Well, we do have the app state, and the app delegate for that matter. How does it get access to those?
This is how it does it:
// get the app delegate
QuizAppDelegate *delegate = (QuizAppDelegate *)
[[ UIApplication sharedApplication] delegate];
AppState * appState = delegate.appState;
I'm not entirely sure of the mechanics of it; but it looks like we can temporarily make it a part of app delegate.
Here it is - it's only temporary, otherwise, I'd probably just move the whole array to appstate:
@interface AppState : NSObject {
int chosenAnswer;
int correctAnswers;
int currentQuestionNumber;
int slotOfCorrectAnswer;
// this will contain the hash table of question objects
NSMutableDictionary *questionHash;
}
@property (nonatomic) int slotOfCorrectAnswer;
With of course the corresponding synthesize.
Ok, now let's check the AnswerController code again.
How does this look:
if (selection == [appState slotOfCorrectAnswer]) {
Oop. Forget to do this in the question controller:
appState.slotOfCorrectAnswer = rand;
Well - a log of the numbers in the comparison match, but it still keeps insisting the answer is incorrect - and always displays the first question's hiragana as the correct one.
Could it have something to do with one of them is an int and the other is an NSUinteger?
Oh, got it. I had forgotten to move the selection++, and the NSlog was happening before hat.
Great! it's working.
No comments:
Post a Comment