Declarations

To manage its relationships and responsibilities, QuizAppDelegate needs five instance variables and two methods. In this section, you will declare these in the QuizAppDelegate header file, QuizAppDelegate.h.

Declaring instance variables

Here are the five instance variables QuizAppDelegate needs:

questions
a pointer to an NSMutableArray containing instances of NSString
answers
a pointer to another NSMutableArray containing instances of NSString
currentQuestionIndex
an int that holds the index of the current question in the questions array
questionField
a pointer to the UILabel object where the current question will be displayed
answerField
a pointer to the UILabel object where the current answer will be displayed

In the project navigator, select QuizAppDelegate.h. Inside the curly braces, add the following declarations for the five instance variables. (Notice the bold type? In this book, code that you need to add is always bold; the code that’s not bold is there to tell you where to type in the new stuff.)

@​i​n​t​e​r​f​a​c​e​ ​Q​u​i​z​A​p​p​D​e​l​e​g​a​t​e​ ​:​ ​N​S​O​b​j​e​c​t​ ​<​U​I​A​p​p​l​i​c​a​t​i​o​n​D​e​l​e​g​a​t​e​>​

{​
 ​ ​ ​ ​i​n​t​ ​c​u​r​r​e​n​t​Q​u​e​s​t​i​o​n​I​n​d​e​x​;​

 ​ ​ ​ ​/​/​ ​T​h​e​ ​m​o​d​e​l​ ​o​b​j​e​c​t​s​
 ​ ​ ​ ​N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​*​q​u​e​s​t​i​o​n​s​;​
 ​ ​ ​ ​N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​*​a​n​s​w​e​r​s​;​

 ​ ​ ​ ​/​/​ ​T​h​e​ ​v​i​e​w​ ​o​b​j​e​c​t​s​ ​-​ ​d​o​n​'​t​ ​w​o​r​r​y​ ​a​b​o​u​t​ ​t​h​i​s​ ​I​B​O​u​t​l​e​t​ ​m​a​c​r​o​,​
 ​ ​ ​ ​/​/​ ​w​e​'​l​l​ ​t​a​l​k​ ​a​b​o​u​t​ ​i​t​ ​s​h​o​r​t​l​y​
 ​ ​ ​ ​I​B​O​u​t​l​e​t​ ​U​I​L​a​b​e​l​ ​*​q​u​e​s​t​i​o​n​F​i​e​l​d​;​
 ​ ​ ​ ​I​B​O​u​t​l​e​t​ ​U​I​L​a​b​e​l​ ​*​a​n​s​w​e​r​F​i​e​l​d​;​
}​
@​p​r​o​p​e​r​t​y​ ​(​n​o​n​a​t​o​m​i​c​,​ ​r​e​t​a​i​n​)​ ​I​B​O​u​t​l​e​t​ ​U​I​W​i​n​d​o​w​ ​*​w​i​n​d​o​w​;​

@​e​n​d​

(Scary syntax? Feelings of dismay? Don’t panic – you will learn more about the Objective-C language in the next chapter. For now, just keep going.)

Declaring methods

Each of the buttons needs to trigger a method in the QuizAppDelegate. A method is a lot like a function – a list of instructions to be executed. Declare two methods in QuizAppDelegate.h after the line that starts with @property. (We will explain @property in Chapter 3; you can ignore it for now.)

@​i​n​t​e​r​f​a​c​e​ ​Q​u​i​z​A​p​p​D​e​l​e​g​a​t​e​ ​:​ ​N​S​O​b​j​e​c​t​ ​<​U​I​A​p​p​l​i​c​a​t​i​o​n​D​e​l​e​g​a​t​e​>​
{​
 ​ ​ ​ ​i​n​t​ ​c​u​r​r​e​n​t​Q​u​e​s​t​i​o​n​I​n​d​e​x​;​

 ​ ​ ​ ​/​/​ ​T​h​e​ ​m​o​d​e​l​ ​o​b​j​e​c​t​s​
 ​ ​ ​ ​N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​*​q​u​e​s​t​i​o​n​s​;​
 ​ ​ ​ ​N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​*​a​n​s​w​e​r​s​;​

 ​ ​ ​ ​/​/​ ​T​h​e​ ​v​i​e​w​ ​o​b​j​e​c​t​s​
 ​ ​ ​ ​I​B​O​u​t​l​e​t​ ​U​I​L​a​b​e​l​ ​*​q​u​e​s​t​i​o​n​F​i​e​l​d​;​
 ​ ​ ​ ​I​B​O​u​t​l​e​t​ ​U​I​L​a​b​e​l​ ​*​a​n​s​w​e​r​F​i​e​l​d​;​
}​
@​p​r​o​p​e​r​t​y​ ​(​n​o​n​a​t​o​m​i​c​,​ ​r​e​t​a​i​n​)​ ​I​B​O​u​t​l​e​t​ ​U​I​W​i​n​d​o​w​ ​*​w​i​n​d​o​w​;​

-​ ​(​I​B​A​c​t​i​o​n​)​s​h​o​w​Q​u​e​s​t​i​o​n​:​(​i​d​)​s​e​n​d​e​r​;​
-​ ​(​I​B​A​c​t​i​o​n​)​s​h​o​w​A​n​s​w​e​r​:​(​i​d​)​s​e​n​d​e​r​;​

@​e​n​d​

Save QuizAppDelegate.h.

What do IBOutlet and IBAction do in the declarations you just entered? They allow you to connect your controller and view objects in the XIB file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.48.181