Preparing the View Controller Outlets and Actions
A
view is connected to its view controller class through outlets and
actions. These must be present in our code files before Interface
Builder will have a clue where to connect our user interface elements,
so let’s work through adding those connection points now.
For this simple project, we’re going to need to interact with three different objects:
The first two provide
input (the field) and output (the label) for the user. The third (the
button) triggers an action in our code to set the contents of the label
to the contents of the text field. Based on what we now know, we can
define the following outlets:
IBOutlet UILabel *userOutput;
IBOutlet UITextField *userInput;
And this action:
-(IBAction)setOutput:(id)sender;
Open the HelloNounViewController.h file in Xcode and add the IBOutlet and IBAction lines. Remember that the outlet directives fall inside the @interface block, and the action should be added immediately following it. Your header file should now resemble this:
#import <UIKit/UIKit.h>
@interface HelloNounViewController : UIViewController {
IBOutlet UILabel *userOutput;
IBOutlet UITextField *userInput;
}
-(IBAction)setOutput:(id)sender;
@end
Congratulations!
You’ve just built the connection points that you’ll need for Interface
Builder to connect to your code. Save the file and get ready to create a
user interface!
Creating the View
Interface Builder makes
designing a user interface (UI) as much fun as playing around in your
favorite graphics application. That said, our emphasis will be on the
fundamentals of the development process and the objects we have at our
disposal. Where it isn’t critical, we move quickly through the interface
creation.
Adding the Objects
The interface for the
HelloNoun application is quite simple—it must provide a space for
output, a field for input, and a button to set the output to the same
thing as the input. Follow these steps to create the UI:
1. | Open HelloNounViewController.xib by double-clicking it within the Xcode project’s Resources folder.
|
2. | If
it isn’t already running, Interface Builder will launch and open the
XIB file, displaying the Document window for the XIB file (see Figure 7). If you don’t see the window, choose Window, Document from the menu.
![](http://programming4.us/image/012011/Using%20the%20View-Based%20Application%20Template_7.jpg)
|
3. | Double-click the icon for the instance of the view (UIView).
The view itself, currently empty, will display. Open the Library by
choosing Tools, Library. Make sure that the Objects button is selected
within the Library—this displays all the components that we can drag
into the view. Your workspace should now resemble Figure 8.
|
4. | Add two labels to the view by clicking and dragging the label (UILabel) object from the Library into the view.
|
5. | The
first label will simply be static text that says Hello. Double-click
the default text that reads Label to edit it and change the content to
read Hello. Position the second label underneath it; this will act as the output area.
For this example, I changed the text of the second label to read
<Noun Goes Here!>. This will serve as a default value until the
user provides a new string. You may need to expand the text labels by
clicking and dragging their handles to create enough room for them to
display.
I also chose to set my labels to align their text to the center. If you
want to do the same, select the label within the view by clicking it,
and then press Command+1 or choose Tools, Attributes Inspector from the
menu. This opens the Attributes Inspector for the label, as demonstrated
in Figure 9.
![](http://programming4.us/image/012011/Using%20the%20View-Based%20Application%20Template_9.jpg)
You may also explore the other attributes to see the effect on the text,
such as style, color, and so on. Your view should now resemble Figure 10.
Figure 10. Add two labels, one static, one to use for output, into the view.
![](http://programming4.us/image/012011/Using%20the%20View-Based%20Application%20Template_10.jpg)
|
6. | When
you’re happy with the results, it’s time to add the elements that the
user will be interacting with: the text field and button. Find the Text
Field object (UITextField) within the Library and click and
drag to position it under your two labels. Using the handles on the
field, stretch it so that it matches the length of your output label.
|
7. | Open
the Attributes Inspector again (Command+1) and set the text size to
match the labels you added earlier. You’ll notice that the field itself
doesn’t get any bigger. This is because the default field type on the
iPhone has a set height. To change the height, click the square-shadowed
“border” button in the inspector. The field will then allow you to
resize its height freely.
|
8. | Finally, click and drag a Round Rect button (UIButton)
from the Library into the view, positioning it right below the text
field. Double-click in the center of the button to add a title to the
button, such as Set Label. Resize the button to fit the label
appropriately. You may also want to again use the Attributes Inspector
to increase the font size.
|
Figure 11 shows our version of this view.
![](http://programming4.us/image/012011/Using%20the%20View-Based%20Application%20Template_11.jpg)
Connecting Outlets and Actions
Our work in Interface
Builder is almost complete. The last remaining step is to connect the
view to the view controller. Because we already created the outlet and
action connection points in the HelloNounViewController.h file, this
will be a piece of cake!
Make sure that you can
see both the Document window and the view you just created. You’re going
to be dragging from the objects in the view to the File’s Owner icon in
the Document window. Why File’s Owner? Because, as you learned earlier,
the XIB is “owned” by the HelloNounViewController object, which is responsible for loading it.
1. | Control-drag
from the File’s Owner icon to the label that you’ve established for
output (titled <Noun Goes Here!> in the example), and then release
the mouse button. You can use either the visual representation of the
label in the view or the listing of the label object within the Document
window.
|
2. | When you release the mouse button, you’ll be prompted to choose the appropriate outlet. Pick userOutput from the list that appears (see Figure 12).
|
3. | Repeat the process for the text field, this time choosing userInput as the outlet. The link between the input and output view objects and the view controller is now established.
|
4. | To finish the view, we still need to connect the button to the setOutput action. Although you could
do this by Control-dragging, it isn’t recommended. Objects may have
dozens of different events that can be used as a trigger. To make sure
that you’re using the right event, you must select the object in the
view, and then press Command+3 or choose Tools, Connections Inspector.
This opens the Connections Inspector, which shows all the possible
events for the object. For a button object, the event that you’re most
likely to want to use is Touch Up Inside, meaning that the user had a
finger on the button, and then released the finger while it was still
inside the button.
|
5. | Make
sure the button is selected, and then drag from the circle beside Touch
Up Inside in the Connections Inspector to the File’s Owner icon. When
prompted for an action, choose setOutput. (It should be the only option.) The Connections Inspector should update to show the completed connection, as shown in Figure 13.
|
Your view is now complete! You can safely exit Interface Builder, saving your changes.