Implementing Methods

Methods and instance variables are declared in the header file (in this case, QuizAppDelegate.h), but the actual code for the methods is placed in the implementation file (in this case, QuizAppDelegate.m). Select QuizAppDelegate.m from the project navigator and type in the following init method. This method creates two arrays and fills them with questions and answers.

@​i​m​p​l​e​m​e​n​t​a​t​i​o​n​ ​Q​u​i​z​A​p​p​D​e​l​e​g​a​t​e​

-​ ​(​i​d​)​i​n​i​t​
{​
 ​ ​ ​ ​/​/​ ​C​a​l​l​ ​t​h​e​ ​i​n​i​t​ ​m​e​t​h​o​d​ ​i​m​p​l​e​m​e​n​t​e​d​ ​b​y​ ​t​h​e​ ​s​u​p​e​r​c​l​a​s​s​
 ​ ​ ​ ​s​e​l​f​ ​=​ ​[​s​u​p​e​r​ ​i​n​i​t​]​;​
 ​ ​ ​ ​i​f​(​s​e​l​f​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​C​r​e​a​t​e​ ​t​w​o​ ​a​r​r​a​y​s​ ​a​n​d​ ​m​a​k​e​ ​t​h​e​ ​p​o​i​n​t​e​r​s​ ​p​o​i​n​t​ ​t​o​ ​t​h​e​m​
 ​ ​ ​ ​ ​ ​ ​ ​q​u​e​s​t​i​o​n​s​ ​=​ ​[​[​N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​a​l​l​o​c​]​ ​i​n​i​t​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​a​n​s​w​e​r​s​ ​=​ ​[​[​N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​a​l​l​o​c​]​ ​i​n​i​t​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​A​d​d​ ​q​u​e​s​t​i​o​n​s​ ​a​n​d​ ​a​n​s​w​e​r​s​ ​t​o​ ​t​h​e​ ​a​r​r​a​y​s​
 ​ ​ ​ ​ ​ ​ ​ ​[​q​u​e​s​t​i​o​n​s​ ​a​d​d​O​b​j​e​c​t​:​@​"​W​h​a​t​ ​i​s​ ​7​ ​+​ ​7​?​"​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​[​a​n​s​w​e​r​s​ ​a​d​d​O​b​j​e​c​t​:​@​"​1​4​"​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​[​q​u​e​s​t​i​o​n​s​ ​a​d​d​O​b​j​e​c​t​:​@​"​W​h​a​t​ ​i​s​ ​t​h​e​ ​c​a​p​i​t​a​l​ ​o​f​ ​V​e​r​m​o​n​t​?​"​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​[​a​n​s​w​e​r​s​ ​a​d​d​O​b​j​e​c​t​:​@​"​M​o​n​t​p​e​l​i​e​r​"​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​[​q​u​e​s​t​i​o​n​s​ ​a​d​d​O​b​j​e​c​t​:​@​"​F​r​o​m​ ​w​h​a​t​ ​i​s​ ​c​o​g​n​a​c​ ​m​a​d​e​?​"​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​[​a​n​s​w​e​r​s​ ​a​d​d​O​b​j​e​c​t​:​@​"​G​r​a​p​e​s​"​]​;​
 ​ ​ ​ ​}​

 ​ ​ ​ ​/​/​ ​R​e​t​u​r​n​ ​t​h​e​ ​a​d​d​r​e​s​s​ ​o​f​ ​t​h​e​ ​n​e​w​ ​o​b​j​e​c​t​
 ​ ​ ​ ​r​e​t​u​r​n​ ​s​e​l​f​;​
}​

In the declarations in QuizAppDelegate.h, neither questions or answers is labeled IBOutlet. The objects that questions and answers point to are created and configured programmatically in the code above instead of by the XIB file.

After the init method, add the two action methods.

-​ ​(​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​
{​
 ​ ​ ​ ​/​/​ ​S​t​e​p​ ​t​o​ ​t​h​e​ ​n​e​x​t​ ​q​u​e​s​t​i​o​n​
 ​ ​ ​ ​c​u​r​r​e​n​t​Q​u​e​s​t​i​o​n​I​n​d​e​x​+​+​;​

 ​ ​ ​ ​/​/​ ​A​m​ ​I​ ​p​a​s​t​ ​t​h​e​ ​l​a​s​t​ ​q​u​e​s​t​i​o​n​?​
 ​ ​ ​ ​i​f​ ​(​c​u​r​r​e​n​t​Q​u​e​s​t​i​o​n​I​n​d​e​x​ ​=​=​ ​[​q​u​e​s​t​i​o​n​s​ ​c​o​u​n​t​]​)​ ​{​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​G​o​ ​b​a​c​k​ ​t​o​ ​t​h​e​ ​f​i​r​s​t​ ​q​u​e​s​t​i​o​n​
 ​ ​ ​ ​ ​ ​ ​ ​c​u​r​r​e​n​t​Q​u​e​s​t​i​o​n​I​n​d​e​x​ ​=​ ​0​;​
 ​ ​ ​ ​}​

 ​ ​ ​ ​/​/​ ​G​e​t​ ​t​h​e​ ​s​t​r​i​n​g​ ​a​t​ ​t​h​a​t​ ​i​n​d​e​x​ ​i​n​ ​t​h​e​ ​q​u​e​s​t​i​o​n​s​ ​a​r​r​a​y​
 ​ ​ ​ ​N​S​S​t​r​i​n​g​ ​*​q​u​e​s​t​i​o​n​ ​=​ ​[​q​u​e​s​t​i​o​n​s​ ​o​b​j​e​c​t​A​t​I​n​d​e​x​:​c​u​r​r​e​n​t​Q​u​e​s​t​i​o​n​I​n​d​e​x​]​;​

 ​ ​ ​ ​/​/​ ​L​o​g​ ​t​h​e​ ​s​t​r​i​n​g​ ​t​o​ ​t​h​e​ ​c​o​n​s​o​l​e​
 ​ ​ ​ ​N​S​L​o​g​(​@​"​d​i​s​p​l​a​y​i​n​g​ ​q​u​e​s​t​i​o​n​:​ ​%​@​"​,​ ​q​u​e​s​t​i​o​n​)​;​

 ​ ​ ​ ​/​/​ ​D​i​s​p​l​a​y​ ​t​h​e​ ​s​t​r​i​n​g​ ​i​n​ ​t​h​e​ ​q​u​e​s​t​i​o​n​ ​f​i​e​l​d​
 ​ ​ ​ ​[​q​u​e​s​t​i​o​n​F​i​e​l​d​ ​s​e​t​T​e​x​t​:​q​u​e​s​t​i​o​n​]​;​

 ​ ​ ​ ​/​/​ ​C​l​e​a​r​ ​t​h​e​ ​a​n​s​w​e​r​ ​f​i​e​l​d​
 ​ ​ ​ ​[​a​n​s​w​e​r​F​i​e​l​d​ ​s​e​t​T​e​x​t​:​@​"​?​?​?​"​]​;​
}​

-​ ​(​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​
{​
 ​ ​ ​ ​/​/​ ​W​h​a​t​ ​i​s​ ​t​h​e​ ​a​n​s​w​e​r​ ​t​o​ ​t​h​e​ ​c​u​r​r​e​n​t​ ​q​u​e​s​t​i​o​n​?​
 ​ ​ ​ ​N​S​S​t​r​i​n​g​ ​*​a​n​s​w​e​r​ ​=​ ​[​a​n​s​w​e​r​s​ ​o​b​j​e​c​t​A​t​I​n​d​e​x​:​c​u​r​r​e​n​t​Q​u​e​s​t​i​o​n​I​n​d​e​x​]​;​

 ​ ​ ​ ​/​/​ ​D​i​s​p​l​a​y​ ​i​t​ ​i​n​ ​t​h​e​ ​a​n​s​w​e​r​ ​f​i​e​l​d​
 ​ ​ ​ ​[​a​n​s​w​e​r​F​i​e​l​d​ ​s​e​t​T​e​x​t​:​a​n​s​w​e​r​]​;​
}​

You will use the default implementations for the rest of the methods, so leave them alone.

Flip back to Figure 1.13. This diagram should make a bit more sense now that you have created all of the objects and connected them 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
18.222.109.4