Graphics Programs Reference
In-Depth Information
Second, don't blindly accept the first suggestion Xcode gives you without verifying it.
Cocoa Touch uses naming conventions, which often cause distinct methods, types, and
variables to have very similar names. Many times, the code-completion will suggestion
something that looks an awful lot like what you want, but it is not the code you are look-
ing for. Always double-check.
Now back to your code. In the declarations in QuizViewController.h , neither
questions or answers is labeled IBOutlet . This is because the objects that ques-
tions and answers point to are created and configured programmatically in the code
above instead of in the XIB file. This is a standard practice: view objects are typically cre-
ated in XIB files, and model objects are always created programmatically.
In addition to the initWithNibName:bundle: method, we need two action methods
for when the buttons are tapped. In QuizViewController.m , add the following code
after the implementation of initWithNibName:bundle: . Make sure this code is be-
fore the @end directive but not inside the curly brackets of the
initWithNibName:bundle: implementation.
...
// Return the address of the new object
return self;
}
- (IBAction)showQuestion:(id)sender
{
// Step to the next question
currentQuestionIndex++;
// Am I past the last question?
if (currentQuestionIndex == [questions count]) {
// Go back to the first question
currentQuestionIndex = 0;
}
// Get the string at that index in the questions array
NSString *question = [questions objectAtIndex:currentQuestionIndex];
// Log the string to the console
NSLog(@"displaying question: %@", question);
// Display the string in the question field
[questionField setText:question];
// Clear the answer field
[answerField setText:@"???"];
}
- (IBAction)showAnswer:(id)sender
{
// What is the answer to the current question?
NSString *answer = [answers objectAtIndex:currentQuestionIndex];
// Display it in the answer field
[answerField setText:answer];
Search WWH ::




Custom Search