MOBILE

iPhone Application Development : Understanding Interface Builder

10/12/2010 9:13:00 AM
Let’s get it out of the way up front: Yes, Interface Builder (or IB for short) does help you create interfaces for your applications, but it isn’t a just a drawing tool for GUIs; it helps you symbolically build application functionality without writing code. This translates to fewer bugs, less development time, and easier-to-maintain projects!

By the Way

At the time this book was being written, Apple began previewing a new version of their developer tools—Xcode 4. In Xcode 4, Interface Builder is part of the development suite, not a standalone application. We are providing an introduction to Xcode 4’s tools in a downloadable document at http://teachyourselfiphone.com.

If you are a beginner, I recommend using the Xcode 3.2 toolset for the time being. It is time tested and, at least compared to the early previews of Xcode 4, includes coding features, such as bookmarks, that Xcode 4 does not.


The Interface Builder Approach

Using Xcode and the Cocoa toolset, you can program iPhone interfaces by hand—instantiating interface objects, defining where they appear on the screen, setting any attributes for the object, and, finally, making them visible. For example, in Introduction to Xcode and the iPhone Simulator", you entered this listing into Xcode to make your iPhone display the text Hello Xcode in the middle of the screen:

myMessage=[[UILabel alloc] initWithFrame:CGRectMake(25.0,225.0,300.0,50.0)];
myMessage.text=@"Hello Xcode";
myMessage.font=[UIFont systemFontOfSize:48];
[window addSubview:myMessage];
[myMessage release];


Imagine how long it would take to build interfaces with text, buttons, images, and dozens of other controls—and think of all the code you’d need to wade through just to make small changes!

Over the years, there have been many different approaches to graphical interface builders. One of the most common implementations is to enable the user to “draw” an interface, but, behind the scenes, create the code that generates that interface. Any tweaks require the code to be edited by hand—hardly an acceptable situation.

Another tactic is to maintain the interface definition symbolically, but to attach the code that implements functionality directly to interface elements. This, unfortunately, means that if you want to change your interface, or swap functionality from one UI element to another, you have to move the code as well.

Interface Builder works differently. Instead of autogenerating interface code or tying source listings directly to interface elements, IB builds live objects that connect to your application code through simple links called connections. Want to change how a feature of your app is triggered? Just change the connection. As you’ll learn a bit later, changing how your application works with the objects you create in Interface Builder is, quite literally, a matter of connecting or reconnecting the dots as you see fit.

The Anatomy of an Interface Builder XIB File

Your work in Interface Builder results in an XML file called an XIB or (for legacy reasons) NIB file, containing a hierarchy of objects. The objects could be interface elements—buttons, toggle switches, and so forth—but might also be other noninterface objects that you need to use in your app. When the XIB file is loaded by your application, the objects described in it are instantiated and can be accessed by your code.

By the Way

Instantiation, just as a quick refresher, is the process of creating an instance of an object that you can work with in your program. An instantiated object gains all the functionality described by its class. Buttons, for example, automatically highlight when clicked, content views scroll, and so on.


The Document Window

What do XIB files look like in IB?  The contents of the file are shown in the IB “Document” window (see Figure 1).

Figure 1. An XIB file’s objects are represented by icons.


By the Way

If you do not see a window with icons when opening the XIB file, choose Window, Document to ensure that the Document window is active and visible on your screen.


In this sample file, three icons are initially visible: File’s Owner, First Responder, and View. The first two are special icons used to represent unique objects in our application; these will be present in all XIB files that you work with:

File’s Owner: The File’s Owner icon denotes the object that loads the XIB file in your running application. This is the object that effectively instantiates all the other objects described in the XIB file. For example, you may have an interface defined in myInterface.xib, which is loaded by an object you’ve written called myInterfaceController. In this case, the File’s Owner would represent the myInterfaceController object.

First Responder: The first responder icon stands for the object that the user is currently interacting with. When a user works with an iPhone application, multiple objects could potentially respond to the various gestures or keystrokes that the user creates. The first responder is the object currently in control and interacting with the user. A text field that the user is typing into, for example, would be the first responder until the user moves to another field or control.

View: The view icon is an instance of the object UIView and represents the visual layout that will be loaded and displayed on the iPhone’s screen. You can double-click view icons to open and edit them with the IB tools, as shown in Figure 2.

Figure 2. Double-click the view icon to open and edit the iPhone application GUI.

Views are hierarchical in nature. This means that as you add controls to your interface, they will be contained within the view. You can even add views within views to cluster controls or create visual elements that can be shown or hidden as a group. Because a view can contain many other objects, I recommend using the list or column view of the Document window to make sure that you can view the full hierarchy of objects you’ve created in an XIB file. To change the document view, click the view mode icon on the Document window toolbar (see Figure 3).

Figure 3. Using the list view mode ensures that you can see all of the objects in your XIB files. Here an XIB with a full interface and view hierarchy is displayed.


Did You Know?

At its most basic level, a view (UIView) is a rectangular region that can contain content and respond to user events (touches and so forth). All the controls (buttons, fields, and so on) that you’ll add to a view are, in fact, subclasses of UIView. This isn’t necessarily something you need to be worried about, except that you’ll be encountering documentation that refers to buttons and other interface elements referred to as subviews and the views that contain them as superviews.

Just keep in the back of your mind that pretty much everything you see on the iPhone screen can be considered a “view” and the terminology will seem a little less alien.


Working with the Document Icons

The Document window shows icons for objects in your application, but what good are they? Aside from presenting a nice list, do the Document window icons provide any functionality?

Absolutely! These icons give you a visual means of referring to the objects they represent. You will interact with the icons by dragging to and from them to create the connections that drive your application’s features.

Consider an onscreen control, such as a button, that needs to be able to trigger an action in your code. By dragging from the button to the File’s Owner icon, you can create a connection from the GUI element you’ve drawn to a method you’ve written in the object that loaded the XIB file.

We’ll go through a hands-on example later this hour so that you can get a feel for how this works. Before we do that, however, let’s take a look at how you go about turning a blank view into an interface masterpiece.

Other  
 
Video
Top 10
Home Theatre Pc Software And Operating Systems (Part 4) - XBMC
Home Theatre Pc Software And Operating Systems (Part 3) - Setting Up Windows Media Center
Home Theatre Pc Software And Operating Systems (Part 2)
Home Theatre Pc Software And Operating Systems (Part 1) - Windows Media Center
Nokia's Extreme Megapixel Bid
Storage, Screens And Sounds (Part 3)
Storage, Screens And Sounds (Part 2)
Storage, Screens And Sounds (Part 1)
Microsoft ASP.NET 4 : Using the SqlProfileProvider (part 4) - The Profile API, Anonymous Profiles
Microsoft ASP.NET 4 : Using the SqlProfileProvider (part 3) - Profiles and Custom Data Types
Most View
Managing and Administering SharePoint 2010 Infrastructure : Using Additional Administration Tools for SharePoint
Binding Application Data to the UI objects in Silverlight
iPhone Application Development : Getting the User’s Attention - Generating Alerts
Understanding and Using Windows Server 2008 R2 UNIX Integration Components (part 2)
iPhone Application Development : Creating and Managing Image Animations and Sliders (part 3) - Finishing the Interface
Cisco Linksys X3000 - The Link to Connectivity
HP LaserJet Pro CM1415fnw - Print from The Clouds
Building Your First Windows Phone 7 Application (part 2) - Using Your First Windows Phone Silverlight Controls
Determine Your Need for Server Core
Mobile Application Security : Bluetooth Security - Overview of the Technology
Using System Support Tools in Vista
Windows 7 : Using Windows Live Calendar (part 3) - Scheduling Appointments and Meetings & Viewing Agendas and Creating To-Do Lists
Advanced ASP.NET : The Entity Framework (part 3) - Handling Errors & Navigating Relationships
Graham Barlow: the Apple view
Ipad : Presentations with Keynote - Adding Transitions (part 2) - Object Transitions
Windows Server 2003 : Troubleshooting Group Policy
Microsoft XNA Game Studio 3.0 : Controlling Color (part 2)
Building the WinPE Image
Programming the Mobile Web : HTML 5 (part 3) - Offline Operation
Windows Phone 7 Development : Using Culture Settings with ToString to Display Dates, Times, and Text