MOBILE

iPhone Application Developmen : Using the View-Based Application Template (part 2) - Preparing the View Controller Outlets and Actions

1/13/2011 11:15:35 AM

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:

  • A text field (UITextField)

  • A label (UILabel)

  • A button (UIButton)

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.

Figure 7. The HelloNounViewController.xib file’s view will contain all the UI objects for the application.


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.

Figure 8. Open the view and the object Library to begin creating the interface.


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.

Figure 9. Use the Attributes Inspector to set the label to center itself and to increase the font size.


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.


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.

Figure 11. Your interface should include two labels, a field, and button—just like this!


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).

Figure 12. Connect the label that will display output to the userOutput outlet.

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.

Figure 13. Use the Connections Inspector to create a link from the event to the action it should trigger.

Your view is now complete! You can safely exit Interface Builder, saving your changes.

Other  
  •  Mobile Application Security: Application Format
  •  Mobile Application Security: Security Testing
  •  Mobile Application Security: The Apple iPhone - Development
  •  Building Android Apps : Installing KiloGap in the Emulator
  •  Building Android Apps : Build KiloGap
  •  Building Android Apps: Create an Android Virtual Device
  •  Building Android Apps: Going Native - Setting Up the Environment
  •  Building Android Apps: Introduction to PhoneGap
  •  iPhone Application Development : How Xcode and Interface Builder Implement MVC
  •  iPhone Application Development : Understanding the Model-View-Controller Paradigm
  •  Building Android Apps: Going Offline - Debugging
  •  Building Android Apps: Creating a Dynamic Manifest File
  •  Building Android Apps: Online Whitelist and Fallback Options
  •  The Basics of the Offline Application Cache
  •  Building Android Apps: Web SQL Database (part 4) - Deleting Rows
  •  Building Android Apps: Web SQL Database (part 3) - Selecting Rows and Handling Result Sets
  •  Building Android Apps: Web SQL Database (part 2) - Inserting Rows
  •  Building Android Apps: Web SQL Database (part 1) - Creating a Database
  •  Building Android Apps: Web Storage
  •  Building Android Apps : Simple Bells and Whistles
  •  
    Top 10
    Nvidia Quadro K5000 Professional Graphics Card (Part 4)
    Nvidia Quadro K5000 Professional Graphics Card (Part 3)
    Nvidia Quadro K5000 Professional Graphics Card (Part 2)
    Nvidia Quadro K5000 Professional Graphics Card (Part 1)
    Mains Cables R US MCRU Music Server - The Mains Thing (Part 2)
    Mains Cables R US MCRU Music Server - The Mains Thing (Part 1)
    Merlin Music TSM MMM Speakers (Part 2)
    Merlin Music TSM MMM Speakers (Part 1)
    System Audio Aura 1 Bookshelf Loudspeaker (Part 2)
    System Audio Aura 1 Bookshelf Loudspeaker (Part 1)
    Most View
    Windows System Programming : Example: Listing File Attributes & Setting File Times
    Windows Azure : Messaging with the queue - Patterns for message processing
    Samsung Galaxy SIII And HTC One X: A Detailed And Thorough Comparison
    Canon EOS 650D: entry-level DSLR with vari-angle capacitive touchscreen
    Windows 7 : Working with the Printer Queue
    Microsoft Sued Comet For Making 94,000 Copies Of Counterfeit Windows
    How To Share Your Shots With Us!
    Asus Vivobook S200E - Stunning Looks And Great Features
    Windows 7 : Working with Multiple Local Group Policy Objects
    Choose The Right Business Broadband (Part 2)
    Network Attached Storage Round-Up (Part 1) - The Benefits Of A NAS
    The best browser hacks (part 1) - Mozilla Firefox
    AG Neovo U-23 : monitor with a 1920x1080 Full HD resolution
    Apple Society – Less Never More
    Educational Software Tools (Part 2)
    Programming Microsoft SQL Server 2005: Using Data Mining Extensions (part 1) - Data Mining Modeling Using DMX
    On Test - Postcard Apps (Part 1)
    100 Ways To Speed Up Windows (Part 4)
    Windows Vista : Permissions and Security (part 1) - Set Permissions for a File or Folder
    .NET Debugging : PowerDbg (part 2) - Send-PowerDbgCommand & Extending PowerDbg