Pulling it all Together

You have created, configured, and connected your view objects and their view controller. You have created the model objects. Two things are left to do for Quiz:

  • finish implementing the action methods showQuestion: and showAnswer: in BNRQuizViewController

  • add code to BNRAppDelegate that will create and display an instance of BNRQuizViewController

Implementing action methods

In BNRQuizViewController.m, finish the implementations of showQuestion: and showAnswer:.

 ​ ​ ​ ​.​.​.​
 ​ ​ ​ ​/​/​ ​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​;​
}​

-​ ​(​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​
 ​ ​ ​ ​s​e​l​f​.​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​ ​(​s​e​l​f​.​c​u​r​r​e​n​t​Q​u​e​s​t​i​o​n​I​n​d​e​x​ ​=​=​ ​[​s​e​l​f​.​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​
 ​ ​ ​ ​ ​ ​ ​ ​s​e​l​f​.​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​ ​=​ ​s​e​l​f​.​q​u​e​s​t​i​o​n​s​[​s​e​l​f​.​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​ ​t​h​e​ ​s​t​r​i​n​g​ ​i​n​ ​t​h​e​ ​q​u​e​s​t​i​o​n​ ​l​a​b​e​l​
 ​ ​ ​ ​s​e​l​f​.​q​u​e​s​t​i​o​n​L​a​b​e​l​.​t​e​x​t​ ​=​ ​q​u​e​s​t​i​o​n​;​

 ​ ​ ​ ​/​/​ ​R​e​s​e​t​ ​t​h​e​ ​a​n​s​w​e​r​ ​l​a​b​e​l​
 ​ ​ ​ ​s​e​l​f​.​a​n​s​w​e​r​L​a​b​e​l​.​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​ ​=​ ​s​e​l​f​.​a​n​s​w​e​r​s​[​s​e​l​f​.​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​ ​l​a​b​e​l​
 ​ ​ ​ ​s​e​l​f​.​a​n​s​w​e​r​L​a​b​e​l​.​t​e​x​t​ ​=​ ​a​n​s​w​e​r​;​
}​

@​e​n​d​

Getting the view controller on the screen

If you were to run Quiz right now, you would not see the interface that you created in BNRQuizViewController.xib. Instead, you would see a blank white screen. To get your interface on screen, you have to connect your view controller with the application’s other controller – BNRAppDelegate.

Whenever you create an iOS application using an Xcode template, an app delegate is created for you. The app delegate is the starting point of an application, and every iOS application has one.

The app delegate manages a single top-level UIWindow for the application. To get the BNRQuizViewController on screen, you need to make it the root view controller of this window.

When an iOS application is launched, it is not immediately ready for the user. There is some setup that goes on behind the scenes. Right before the app is ready for the user, the app delegate receives the message application:didFinishLaunchingWithOptions:. This is your chance to prepare the application for action. In particular, you want to make sure that your interface is ready before the user has a chance to interact with it.

In the project navigator, find and select BNRAppDelegate.m. Add the following code to the application:didFinishLaunchingWithOptions: method to create an instance of BNRQuizViewController and to set it as the root view controller of the app delegate’s window.

#​i​m​p​o​r​t​ ​"​B​N​R​A​p​p​D​e​l​e​g​a​t​e​.​h​"​
#​i​m​p​o​r​t​ ​"​B​N​R​Q​u​i​z​V​i​e​w​C​o​n​t​r​o​l​l​e​r​.​h​"​

-​ ​(​B​O​O​L​)​a​p​p​l​i​c​a​t​i​o​n​:​(​U​I​A​p​p​l​i​c​a​t​i​o​n​ ​*​)​a​p​p​l​i​c​a​t​i​o​n​
 ​ ​ ​ ​ ​ ​ ​ ​d​i​d​F​i​n​i​s​h​L​a​u​n​c​h​i​n​g​W​i​t​h​O​p​t​i​o​n​s​:​(​N​S​D​i​c​t​i​o​n​a​r​y​ ​*​)​l​a​u​n​c​h​O​p​t​i​o​n​s​
{​
 ​ ​ ​ ​s​e​l​f​.​w​i​n​d​o​w​ ​=​ ​[​[​U​I​W​i​n​d​o​w​ ​a​l​l​o​c​]​ ​i​n​i​t​W​i​t​h​F​r​a​m​e​:​[​[​U​I​S​c​r​e​e​n​ ​m​a​i​n​S​c​r​e​e​n​]​ ​b​o​u​n​d​s​]​]​;​
 ​ ​ ​ ​/​/​ ​O​v​e​r​r​i​d​e​ ​p​o​i​n​t​ ​f​o​r​ ​c​u​s​t​o​m​i​z​a​t​i​o​n​ ​a​f​t​e​r​ ​a​p​p​l​i​c​a​t​i​o​n​ ​l​a​u​n​c​h​

 ​ ​ ​ ​B​N​R​Q​u​i​z​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​*​q​u​i​z​V​C​ ​=​ ​[​[​B​N​R​Q​u​i​z​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​a​l​l​o​c​]​ ​i​n​i​t​]​;​
 ​ ​ ​ ​s​e​l​f​.​w​i​n​d​o​w​.​r​o​o​t​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​=​ ​q​u​i​z​V​C​;​

 ​ ​ ​ ​s​e​l​f​.​w​i​n​d​o​w​.​b​a​c​k​g​r​o​u​n​d​C​o​l​o​r​ ​=​ ​[​U​I​C​o​l​o​r​ ​w​h​i​t​e​C​o​l​o​r​]​;​
 ​ ​ ​ ​[​s​e​l​f​.​w​i​n​d​o​w​ ​m​a​k​e​K​e​y​A​n​d​V​i​s​i​b​l​e​]​;​
 ​ ​ ​ ​r​e​t​u​r​n​ ​Y​E​S​;​
}​

Now whenever the app is launched, an instance of BNRQuizViewController will be created. This instance will then receive the initWithNibName:bundle: message, which will trigger loading the NIB file compiled from BNRQuizViewController.xib and the creation of the model objects.

Your Quiz is complete. Time to try it out.

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

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