Appendix C. Getting Around in Interface Builder

Interface Builder is one of the tools shipped with the iPhone SDK. It is a visual design tool that you can use to build the user interface of your iPhone applications. Although not strictly required for the development of your iPhone applications, Interface Builder plays an integral role in your journey of learning about iPhone application development. This appendix covers some of the important features of Interface Builder.

.XIB WINDOW

The most direct way to launch Interface Builder is to double-click any of the .xib files in your Xcode project. For example, if you have created a View-based Application project, there would be two .xib files in the Resources folder of Xcode. Double-clicking any one of them launches Interface Builder.

When Interface Builder is launched, the first window that you see has the same name as your .xib file (see Figure C-1).

Figure C-1

Figure C.1. Figure C-1

Within this window are several items, and depending on what you double-clicked, you should see some of the following items, such as:

  • File's Owner

  • First Responder

  • View, Table View, Window, and so on

  • Some View Controllers and delegates

By default, the .xib window is displayed in icon mode. But you can also switch to list mode, where you can view some of the items in more detail. For example, Figure C-2 shows that when viewed in list mode, the View item displays a hierarchy of views contained within it.

Figure C-2

Figure C.2. Figure C-2

DESIGNING THE VIEW

To design the user interface of your application, you typically double-click the View (or Table View or other) item on the .xib window to visually display the window so that you can drag and drop views onto your View window.

To populate your View window with views, you drag and drop objects listed in the Library window (see the Library section for more information on the Library window). Figure C-3 shows a Label view being dropped onto the View window.

Figure C-3

Figure C.3. Figure C-3

As you position the view on the window, gridlines appear to guide you.

The View window also allows you to rotate the orientation of your view so that you can see how your view looks when it is rotated to the landscape orientation (see Figure C-4).

Figure C-4

Figure C.4. Figure C-4

INSPECTOR WINDOW

To customize the various attributes and properties of views, Interface Builder provides what it calls the Inspector window. The Inspector window is divided into four panes:

  • Attributes Inspector

  • Connection Inspector

  • Size Inspector

  • Identity Inspector

You can invoke the Inspector window by choosing Tools

INSPECTOR WINDOW

The following sections discuss each of the Inspector panes in more details.

Attributes Inspector pane

The Attributes Inspector window (see Figure C-6) is where you configure the attributes of views in Interface Builder. The content of the Attributes Inspector window is dynamic and will change depending on what is selected in the View window.

Figure C-6

Figure C.6. Figure C-6

You invoke the Attributes Inspector window by choosing Tools

Figure C-6

Connections Inspector window

The Connections Inspector window (see Figure C-7) is where you connect the outlets and actions to your View Controller in Interface Builder. The content of the Connections Inspector window is dynamic and will change depending on what is selected in the View window.

Figure C-7

Figure C.7. Figure C-7

You invoke the Connections Inspector window by choosing Tools

Figure C-7

Size Inspector window

The Size Inspector window (see Figure C-8) is where you configure the size and positioning of views in Interface Builder.

Figure C-8

Figure C.8. Figure C-8

The Size Inspector window can be invoked by choosing Tools

Figure C-8

Identity Inspector window

The Size Inspector window (see Figure C-9) is where you configure the outlets and actions of your view controller. You add actions and outlets by clicking the plus sign (+) button and remove them using the minus sign button(–). The last section in this appendix discusses in more detail how to create outlets and actions in Interface Builder.

Figure C-9

Figure C.9. Figure C-9

You invoke the Identity Inspector window by choosing Tools

Figure C-9

LIBRARY

The Library (Tools

LIBRARY
Figure C-10

Figure C.10. Figure C-10

You can configure the Library to display its views in different modes (see also Figure C-11):

  • Icons

  • Icons and Labels (which is the mode shown in Figure C-10)

  • Icons and Descriptions

Figure C-12 shows the Library displayed in the Icons and Descriptions mode.

Figure C-11

Figure C.11. Figure C-11

Figure C-12

Figure C.12. Figure C-12

OUTLETS AND ACTIONS

Outlets and actions are fundamental mechanisms in iPhone programming through which your code can connect to the views in your user interface (UI). When you use outlets, your code can programmatically reference the views on your UI, with actions serving as event handlers that handle the various events fired by the various views.

Although you can write code to connect actions and outlets, Interface Builder simplifies the process by allowing you to connect outlets and actions using the drag-and-drop technique.

Creating Outlets and Actions

To create an action, you can click the plus sign (+) button that appears in the Class Actions section of the Identity Inspector window (see left side of Figure C-13). Remember to include the colon (:) character at the end of the action name. This character allows the action to have an input parameter of type id, like this:

-(IBAction) myAction:(id) sender;
Figure C-13

Figure C.13. Figure C-13

Be sure to note that in Figure C-13 both the outlet and action are listed under the HelloWorldViewController.xib header. This is because the outlet and action are defined within Interface Builder.

Likewise, click the plus sign (+) button in the Class Outlets section of the Identity Inspector window to create an outlet. It is always good to specify the type for the outlet that you are defining. Using the preceding sample, if you want the outlet to connect to a UITextField view, you should specify the type of myOutlet1 as UITextField rather than id.

After the outlet and action are created using Interface Builder, you still need to define them in the .h file, like this:

#import <UIKit/UIKit.h>

@interface HelloWorldViewController : UIViewController {

IBOutlet UITextField *myOutlet1;

}

-(IBAction) myAction1:(id) sender;

@end

Note

Whether you are in Xcode or Interface Builder, be sure to save the file after you have modified it.

Going back to the Identity Inspector window in Interface Builder, observe again that both the outlet and action are now listed under the HelloWorldViewController.h header (see Figure C-14). If you now try to click the plus sign (+) button again, the outlet or action will be listed under the HelloWorldViewController.xib header until you define them in the .h file.

Figure C-14

Figure C.14. Figure C-14

Note

Outlets and actions defined in the .h file cannot be deleted in the Identity Inspector window by clicking the minus sign (−) button.

Actually, a much simpler way is to define the outlets and actions directly in the .h files of your view controllers first. That saves you the trouble of defining them in the Identity Inspector window of the Interface Builder.

Alternately, if you do not want to manually type in the declaration of the outlets and actions in your View Controller class, you can create the outlets and actions in the Identity Inspector window, select the File's Owner item, and then choose File

Figure C-14

Take note that for code generated by Interface Builder, the outlets will not be exposed as properties. You must manually add the code to expose the properties using the @property keyword and the @synthesize keyword to generate the getters and setters for the properties.

Note

In general, it is always easier to define the outlets and actions manually rather than have Interface Builder do it for you.

Connecting Outlets and Actions

To connect the outlets and actions to the views, you have two options, both which are discussed in the following sections.

Method 1

For connecting outlets, Control-click and drag the File's Owner item to the view to which you want to connect (see Figure C-15).

Figure C-15

Figure C.15. Figure C-15

When you release the mouse button, a list appears from which you can select the correct outlet. When defining your outlets (in the Identity Inspector window, or in code), remember that you can specify the type of view your outlet is referring to. When you release the mouse button, Interface Builder lists only the outlets that match the type of view you have selected. For example, if you defined myOutlet1 as UIButton and you Control-click and drag the File's Owner item to the Text Field view on the View window, the myOutlet1 will not appear in the list of outlets.

For connecting actions, Control-click and drag the view to the File's Owner item in the .xib window (see Figure C-16).

Figure C-16

Figure C.16. Figure C-16

When you release the mouse button, a list appears from which you can select the correct action.

When you have connected the outlets and actions, a good practice is to view all the connections in the File's Owner item by right-clicking it. Figure C-17 shows that the File's Owner item is connected to the Text Field view through the myOutlet1 outlet, and the Button's Touch Up Inside event is connected to the myAction1: action.

Figure C-17

Figure C.17. Figure C-17

How does the Button know that it is the Touch Up Inside event (and not other events) that should be connected to the myAction1: action when you Control-click and drag the Button to the File's Owner item? Well, the Touch Up Inside event is such a commonly used event that it is the default event selected when you perform a Control-click and drag action. What if you want to connect other events other than the default event? The second method shows you how.

Method 2

An alternative method for connecting outlets is to right-click the File's Owner item and connect the outlet to the view directly (see Figure C-18).

Figure C-18

Figure C.18. Figure C-18

For connecting actions, you can connect the relevant action with the views to which you want to connect (see Figure C-19). When you release the mouse button, the list of available events appears, and you can select the event you want.

Figure C-19

Figure C.19. Figure C-19

Alternatively, you can right-click the view in question and connect the relevant events to the File's Owner item (see Figure C-20). When you release the mouse button, a list of actions is shown. Select the action to which you want to connect.

Figure C-20

Figure C.20. Figure C-20

SUMMARY

In this appendix, you had a quick overview of how to use Interface Builder to build the user interface of your iPhone application. The section on outlets and actions discussed the different ways in which you can connect outlets and actions to the views in your application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.146.199