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
In BNRQuizViewController.m, finish the implementations of showQuestion: and showAnswer:.
... // Return the address of the new object return self; } - (IBAction)showQuestion:(id)sender { // Step to the next question self.currentQuestionIndex++; // Am I past the last question? if (self.currentQuestionIndex == [self.questions count]) { // Go back to the first question self.currentQuestionIndex = 0; } // Get the string at that index in the questions array NSString *question = self.questions[self.currentQuestionIndex]; // Display the string in the question label self.questionLabel.text = question; // Reset the answer label self.answerLabel.text = @"???"; } - (IBAction)showAnswer:(id)sender { // What is the answer to the current question? NSString *answer = self.answers[self.currentQuestionIndex]; // Display it in the answer label self.answerLabel.text = answer; } @end
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.
#import "BNRAppDelegate.h" #import "BNRQuizViewController.h" - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch BNRQuizViewController *quizVC = [[BNRQuizViewController alloc] init]; self.window.rootViewController = quizVC; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; }
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.