Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning iOS5 Development.pdf
Скачиваний:
7
Добавлен:
09.05.2015
Размер:
15.6 Mб
Скачать

34CHAPTER 2: Appeasing the Tiki Gods

File template library: This section contains a collection of file templates you can use when you need to add a new file to your project. For example, if you want to add a new Objective-C class to your project, drag an Objective-C class file from the file template library.

Code snippet library: This section features a collection of code snippets you can drag into your source code files. Can’t remember the syntax for Objective-C fast enumeration? That’s fine—just drag that particular snippet out of the library, and you don’t need to look it up. Have you written something you think you’ll want to use again later? Select it in your text editor and drag it to the code snippet library.

Object library: This section is filled with reusable objects, such as text fields, labels, sliders, buttons, and just about any object you would ever need to design your iOS interface. We’ll use the object library extensively in this book to build the interfaces for our sample programs.

Media library: As its name implies, this section is for all your media, including pictures, sounds, and movies.

NOTE: The items in the object library are primarily from the iOS UIKit, which is a framework of

objects used to create an app’s user interface. UIKit fulfills the same role in Cocoa Touch as AppKit does in Cocoa. The two frameworks are similar conceptually, but because of differences in the platforms, there are obviously many differences between them. On the other hand, the Foundation framework classes, such as NSString and NSArray, are shared between Cocoa

and Cocoa Touch.

Note the search field at the bottom of the library. Do you want to find a button? Type button in the search field, and the current library will show only items with button in the name. Don’t forget to clear the search field when you are finished searching.

Adding a Label to the View

Let’s give Interface Builder a try. Click the object library icon (it looks like a cube) at the top of the library to bring up the object library. Now scroll through the library to find a Table View. That’s it—keep scrolling, and you’ll find it. Or wait! There’s a better way: just type the words Table View in the search field. Isn’t that so much easier?

TIP: Here’s a nifty shortcut: press ^ 3 to jump to the search field and highlight its contents.

Now find a Label in the library. It is likely on or near the top of the list. Next, drag the label onto the view we saw earlier. (If you don’t see the view in your editor pane, click

www.it-ebooks.info

CHAPTER 2: Appeasing the Tiki Gods

35

the View icon in the Interface Builder dock.) As your cursor appears over the view, it will turn into the standard, “I’m making a copy of something” green plus sign you know from the Finder. Drag the label to the center of the view. A pair of blue guidelines—one vertical and one horizontal—will appear when your label is centered. It’s not vital that the label be centered, but it’s good to know those guidelines are there. Figure 2–17 shows what our workspace looked like just before we released our drag.

Figure 2–17. We’ve found a label in our library and dragged it onto our view. Note that we typed label into the library search field to limit our object list to those containing the word label.

User interface items are stored in a hierarchy. Most views can contain subviews, though there are some, like buttons and most other controls, that can’t. Interface Builder is smart. If an object does not accept subviews, you will not be able to drag other objects onto it.

We’ll add our label as a subview of our main view (the view named View), which will cause it to show up automatically when that view is displayed to the user. Dragging a Label from the library to the view called View adds an instance of UILabel as a subview of our application’s main view.

Let’s edit the label so it says something profound. Double-click the label you just created, and type the text Hello, World! Next, click off the label, and then reselect it and drag the label to recenter it, or position it wherever you want it to appear on the screen.

Guess what? Once we save, we’re finished. Select File Save, or press S. Then click the popup menu at the upper left of the Xcode workspace window and choose iPhone Simulator (the popup might also include a version number—choose the latest and greatest) so that our app will run in the simulator. If you are a member of Apple’s paid iOS Developer Program, you can try running your app on your phone. In this book, we’ll

www.it-ebooks.info

36

CHAPTER 2: Appeasing the Tiki Gods

stick with the simulator as much as possible, since running in the simulator doesn’t require any paid membership.

Ready to run? Select Product Run or press R. Xcode will compile your app and launch it in the iPhone simulator, as shown in Figure 2–18.

NOTE: If your iOS device is connected to your Mac when you build and run, things might not go quite as planned. In a nutshell, in order to be able to build and run your applications on your iPhone, iPad, or iPod touch, you must sign up and pay for one of Apple’s iOS Developer Programs, and then go through the process of configuring Xcode appropriately. When you join the program, Apple will send you the information you’ll need to get this done. In the meantime, most of the programs in this book will run just fine using the iPhone or iPad simulator.

Figure 2–18. Here’s the Hello, World program in its full iPhone glory!

When you are finished admiring your handiwork, you can head back over to Xcode. Xcode and the simulator are separate applications.

www.it-ebooks.info

CHAPTER 2: Appeasing the Tiki Gods

37

TIP: You are welcome to quit the simulator once you finish examining your app, but you’ll just be restarting it in a moment. If you leave the simulator running and ask Xcode to run your application again, Xcode will ask you if you want to stop your existing app first or run the app as

a second instance, leaving the first instance running as well. If this seems confusing, feel free to

quit the simulator each time you finish testing your app. No one will know!

Wait a second! That’s it? But we didn’t write any code. That’s right.

Pretty neat, huh?

Well, how about if we wanted to change some of the properties of the label, like the text size or color? We would need to write code to do that, right? Nope. Let’s see just how easy it is to make changes.

Changing Attributes

Head back to Xcode and single-click the Hello World label so that it is selected. Now turn your attention to the area above the library pane. This part of the utility pane is called the inspector. Like the library, the inspector pane is topped by a series of icons, each of which changes the inspector to view a specific type of data. To change the attributes of the label, we’ll need the fourth icon from the left, which brings up the object attributes inspector, as shown in Figure 2–19.

TIP: The inspector, like the project navigator, has keyboard shortcuts corresponding to each of its icons. The inspector’s keyboard shortcuts start with 1 for the leftmost icon, 2 for the next icon, and so on. Unlike the project navigator, the number of icons in the inspector is context-sensitive and changes depending on which object is selected in the navigator and/or

editor.

www.it-ebooks.info

38

CHAPTER 2: Appeasing the Tiki Gods

Figure 2–19. The object attributes inspector showing our label’s attributes

Go ahead and change the label’s appearance to your heart’s delight. Feel free to play around with the font, size, and color of the text. Note that if you increase the font size, you may need to resize the label itself to make room for larger text. Once you’re finished

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]