Graphics Programs Reference
In-Depth Information
Implementing Methods
Methods and instance variables are declared in the header file (in this case,
QuizViewController.h ), but the actual code for the methods is placed in the imple-
mentation file (in this case, QuizViewController.m ). Select QuizViewControl-
ler.m in the project navigator.
When you create a new application in Xcode , the template fills in a lot of boiler-plate code.
This code may be useful for you later on, but for now, it is distracting. So we're going to
remove it. In QuizViewController.m , delete everything between the @implement-
ation and @end directives so that QuizViewController.m looks like this:
@implementation QuizViewController
@end
When the application launches, the QuizViewController will be sent the message
initWithNibName:bundle: . In QuizViewController.m , implement the
initWithNibName:bundle: method by adding the following code that creates two
arrays and fills them with questions and answers.
@implementation QuizViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
// Call the init method implemented by the superclass
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Create two arrays and make the pointers point to them
questions = [[NSMutableArray alloc] init];
answers = [[NSMutableArray alloc] init];
// Add questions and answers to the arrays
[questions addObject:@"What is 7 + 7?"];
[answers addObject:@"14"];
[questions addObject:@"What is the capital of Vermont?"];
[answers addObject:@"Montpelier"];
[questions addObject:@"From what is cognac made?"];
[answers addObject:@"Grapes"];
}
// Return the address of the new object
return self;
}
@end
As you work through this topic, you will type a lot of code. Notice that as you were typing
this code, Xcode was ready to fill in parts of it for you. For example, when you started typ-
Search WWH ::




Custom Search