When the Quiz application loads its interface from MainWindow.xib, the objects that make up the interface are floating around in memory. The QuizAppDelegate (a controller object) needs to know where the labels (view objects) are in memory so that it can tell them what to display. The buttons (view objects that the user interacts with) need to know where the QuizAppDelegate is so that they can report when they are tapped. Your objects need connections. A connection lets an object know where another object is in memory.
Figure 1.14 shows the connections for Quiz. Some have already been made by the template (between the window outlet of QuizAppDelegate and the UIWindow instance, for example), and some were made implicitly (dragging a view object onto the window sets up a connection between the view and the window). However, you still have a few more connections to make to get your objects communicating properly.
Here are the missing connections:
Let’s start with the connections to the UILabel instances. The instance of QuizAppDelegate has a pointer called questionField. You want questionField to point to the instance of UILabel at the top of the window. In MainWindow.xib, right-click or Control-click on the QuizAppDelegate in the outline view to bring up the connections panel (Figure 1.15). Then drag from the circle beside questionField to the UILabel.
(If you do not see questionField here, double-check your QuizAppDelegate.h file for typos. Did you end each line with a semicolon? Have you saved the file since you added questionField?)
When the XIB file is loaded (for MainWindow.xib, this is when the application launches), the QuizAppDelegate’s questionField pointer will now automatically point to this instance of UILabel.
Next, drag from the circle beside answerField to the other UILabel (Figure 1.16).
Notice that you drag from the object with the pointer to the object that you want that pointer to point at. Also, notice that the pointers that appear in the connections panel are the ones that have been decorated with IBOutlet in QuizAppDelegate.h.
When a UIButton is tapped, it sends a message to another object. The object that is sent the message is called the target. The message is called the action, and it is the name of the method that tapping the button should trigger. So the button needs answers to two questions: “Who’s the target?” and “What’s the action?” For the button, the target should be QuizAppDelegate, and the action should be showQuestion:.
To set an object’s target and action, you Control-drag from the object to its target. When you release the mouse, the target is set, and a pop-up menu appears that lets you choose the action. Select the QuizAppDelegate. Once QuizAppDelegate is highlighted, release the mouse button and choose showQuestion: from the pop-up menu, as shown in Figure 1.17. Notice that the choices in this menu are the methods you decorated with IBAction in QuizAppDelegate.h.
button and Control-drag (or right-drag) to theNow set the target and action of the Show Answer button. Control-drag from the button to the QuizAppDelegate and choose showAnswer: from the pop-up menu (Figure 1.18).
There are now six connections between your QuizAppDelegate and other objects. You’ve set its pointers answerField and questionField to point at the labels. That’s two. The QuizAppDelegate is the target for both buttons. That’s four. The project’s template made two additional connections. First, the UIApplication object (File's Owner in this XIB file) has a pointer called delegate which points at the QuizAppDelegate; we’ll discuss this somewhat complex relationship in Chapter 4. Second, the window pointer of your QuizAppDelegate was set to the instance of UIWindow. That makes six.
You can check these connections in the connections inspector. Select the QuizAppDelegate in the outline view and then click the icon in the inspector selector to reveal the connections inspector in the utilities area. (Figure 1.19).
Your XIB file is complete. The view objects and the one controller object have been created, the views have been configured, and all the necessary connections have been made. Save your XIB file, and let’s move on to writing the methods.
3.137.212.71