Lập trình Wrox Professional Xcode 3 cho Mac OS part 31 doc

8 71 0
Lập trình Wrox Professional Xcode 3 cho Mac OS part 31 doc

Đang tải... (xem toàn văn)

Thông tin tài liệu

236 ❘ CHAPTER 13 INTERFACE BUILDER Interface Builder provides a “ scratch pad ” user defaults fi le for each simulated nib document. This transient set of preferences can be set to be periodically, or manually, erased from the Simulator tab of the Interface Builder preferences. Erasing the preferences clears all user default values, as though the application was being run for the fi rst time. Building Your Project from Interface Builder If you look at the workfl ow diagram in Figure 13 - 2, you ’ ll see that it ’ s very typical to write code, make connections in Interface Builder, switch back to Xcode, and then build and test your application. Interface Builder provides a shortcut: the File ➪ Build and Go in Xcode (Shift+Command+R) command switches back to Xcode, builds your project, and launches it. INTERFACE BUILDER ’ S INTERFACE This section starts with a brief tour of Interface Builder ’ s interface. One reason it will be brief, and also one of the reasons Interface Builder is so easy to use, is that there are basically only three interface elements: the nib document window, the inspector palette, and the library palette. All three are shown in Figures 13 - 4 and 13 - 5. FIGURE 13-4 c13.indd 236c13.indd 236 1/21/10 3:36:22 PM1/21/10 3:36:22 PM Download at getcoolebook.com FIGURE 13-6 Interface Builder’s Interface ❘ 237 Figure 13 - 4 shows a Cocoa nib document, and Figure 13 - 5 shows a Cocoa Touch (iPhone/iPod) nib document. When you open a nib document, the contents of the fi le are represented abstractly in the nib document window, as shown in the lower left of Figures 13 - 4 and 13 - 5. Nib documents contain objects, and those objects are shown here either as icons, hierarchically, or in a browser. You select which view you want to see in the toolbar. Figure 13 - 6 shows the contents of a nib document in all three views. The icon view only shows the top - level objects. You will also fi nd a few special objects listed; these are described later in the section “ Placeholder Objects. ” FIGURE 13-5 The two important palettes are the inspector palette and the library palette, both described next. Most of the other windows you ’ ll be working with will be view windows. View windows present your view objects (approximately) as they will appear in your application: a window is shown as a window, a menu as a menu, a button as a button. You can edit your objects in either the nib document or manipulate their visual representation in a view window. The effect is the same. c13.indd 237c13.indd 237 1/21/10 3:36:24 PM1/21/10 3:36:24 PM Download at getcoolebook.com 238 ❘ CHAPTER 13 INTERFACE BUILDER Inspector Palette Shown in the middle of Figures 13 - 4 and 13 - 5 is the inspector palette. The inspector palette shows the properties and settings of the currently selected object (or objects). It ’ s where you customize and connect objects. These activities are described in the sections “ Confi guring Objects ” and “ Connecting Objects. ” Much of your time will be spent using the inspector palette to fi ne - tune your objects. Several different inspectors all occupy the same palette window; changing inspectors just changes the view of the single inspector palette window. You can change inspectors using the tabs at the top of the inspector window or using the menu commands in the Tool menu. You ’ ll notice that there are different sets of inspectors for the Cocoa and iPhone nib documents. Only inspectors appropriate to the nib document architecture appear in the palette. For instance, the iPhone OS (as of this writing) does not support bindings, so there is no bindings inspector when working with a Cocoa Touch (iPhone) nib document. Library Palette On the right in Figures 13 - 4 and 13 - 5 is the library palette. The library palette is your source for new objects and other resources. Creating a new object is described in the section “ Creating Objects. ” The library is organized into groups, selectable using the tab control at the top of the palette window. To create a window, for example, just drag a window object out of the library palette and drop it anywhere in the screen. To add a button to that window, drag a button object off the palette and drop it into the window. The library palette ’ s Classes view lists all of the classes that Interface Builder knows about. While many of these classes are represented in the Objects view, the Classes view also includes all custom classes you ’ ve defi ned and is organized by name rather than by function. Either view can be used as a source of new objects. In addition to missing inspector palettes, the active nib document architecture may cause other Interface Builder interface elements to appear or disappear. Carbon nib documents, for example, do not support creating objects with arbitrary types (classes), so there will be no Classes tab in the Library palette when a Carbon document is active. The Media tab represents content fi les (mostly images) to which objects can refer in your nib document. These include system resources, like standard button icons and sounds, as well as any resources you ’ ve added to your project. The library palette presents each list in one or more panes. In the objects list, the top portion lets you browse and select categories of objects. The individual objects in the selected categories are displayed in the middle pane. The bottom pane presents interesting information about the selected objects. The layout of the list can be controlled with the pop - up menu at the bottom of the palette. The other lists have similar features and controls. c13.indd 238c13.indd 238 1/21/10 3:36:26 PM1/21/10 3:36:26 PM Download at getcoolebook.com If you know the name, or part of the name, of the class or media object you ’ re looking for, type it into the search fi eld at the bottom of the window, as shown in Figure 13 - 7. Multiple NIB Document Windows Like Xcode projects, Interface Builder always has one active nib document. Also like Xcode, the Window ➪ Document (Command+0) command brings forward the currently active document window. To aid you in identifying the view windows contained in that nib document, Interface Builder dims any view objects that are not part of the active nib document. Figure 13 - 8 shows two nib documents: one containing a Cocoa window and menu and the other containing a Cocoa Touch view. The Cocoa document is active. This dims all of the windows belonging to the other nib document. Creating a NIB Document ❘ 239 CREATING A NIB DOCUMENT Interface Builder edits nib documents, so the fi rst thing you must do is create a nib document. If you created your project in Xcode using one of the standard templates, you probably already have a nib document in your project. Open the nib document in the source group and Xcode launches Interface Builder. Also, many new fi le templates in Xcode also create companion nib documents. For example, creating a new iPhone view controller class in Xcode also adds its associated nib document to the project for you. FIGURE 13-7 FIGURE 13-8 c13.indd 239c13.indd 239 1/21/10 3:36:32 PM1/21/10 3:36:32 PM Download at getcoolebook.com 240 ❘ CHAPTER 13 INTERFACE BUILDER If you already have a nib document, or created a new one using Xcode, you can skip this section. Come back when you need to create one. Choosing a Template If you need to create additional nib documents, launch or switch to Interface Builder and choose the File ➪ New command. Interface Builder presents the template assistant shown in Figure 13 - 9. FIGURE 13-9 COCOA TEMPLATES CONTENTS Application Makes a Cocoa nib document containing a standard Cocoa application menu bar and an empty window. This template is appropriate for a “single window” (non-document based) application. However, you should rarely need this template — all Xcode project templates for Cocoa applications already include a nib document containing the same thing. Empty Creates an empty Cocoa nib document. Choose a template from the list. The templates are organized into groups, depending on the SDKs you have installed. This can include Cocoa, Cocoa Touch (iPhone/iPod), Carbon, and IB Kit (Interface Builder Kit) groups. All of the templates in the Cocoa group create a Cocoa nib document; Carbon group templates create Carbon nib documents, and so on. You cannot change or convert between document types, so make sure you choose a template from the correct group. The following tables provide a brief summary of the templates. c13.indd 240c13.indd 240 1/21/10 3:36:32 PM1/21/10 3:36:32 PM Download at getcoolebook.com Creating a NIB Document ❘ 241 COCOA TEMPLATES CONTENTS Main Menu Contains a standard application menu bar. Again, you’re unlikely to use this template unless you’re creating alternate menu sets for your application. View Contains a single NSView object. Useful for defi ning a subview. You can populate it to include any combination of standard and custom view objects (you can change the class of the view object to one of your own). Window Creates a Cocoa nib document containing a single, empty, NSWindow object. COCOA TOUCH TEMPLATES CONTENTS Application Creates a Cocoa Touch nib document containing a UIWindow and an application delegate object. Again, you should rarely need this template — the Xcode project templates for an iPhone application will already include a nib document containing a much more specifi c set of objects appropriate to the type of application you’re creating. Empty Creates an empty Cocoa Touch nib document. View Contains a single UIView object. Useful for defi ning a complete view, subview, or a reusable cell (in the iPhone framework, a cell is a subclass of UIView). You can expand it to include any combination of standard and custom view objects. Window Creates a Cocoa Touch nib document containing a single, empty, UIWindow object. CARBON TEMPLATES CONTENTS Application Creates a Carbon nib document containing a Carbon application menu bar and an empty window. This template is appropriate for a “single window” (non-document based) application. Dialog A single Movable Modal window. Empty An empty Carbon nib document. Main Menu Contains a standard application menu bar. Again, you’re unlikely to use this template unless you’re creating alternate menu sets for your application. Window A single Carbon window. c13.indd 241c13.indd 241 1/21/10 3:36:33 PM1/21/10 3:36:33 PM Download at getcoolebook.com 242 ❘ CHAPTER 13 INTERFACE BUILDER Interface Builder still supports legacy Carbon nib documents, but support for Carbon (pure C) programming and technologies is being systematically removed from both the operating system and the Xcode development tools in favor of the more modern Cocoa technologies. This chapter focuses almost exclusively on development in a Cocoa environment. For more information about Carbon nib documents, refer to the Carbon Resources Programming Guide at http://developer.apple.com/mac/library/documentation/Cocoa/ Conceptual/LoadingResources/CarbonNibs/. IB KIT TEMPLATES CONTENTS inspector Creates a Cocoa nib document containing an inspector palette subview. Use this template to create your own extensions to Interface Builder. See the “ Customizing Interface Builder ” section for more details. Library Creates a Cocoa nib document containing the NSView objects used to present a custom Interface Builder object in the library palette. When you have selected your desired template, click the New button. Alternatively, you can avoid creating a new fi le and open an existing document by clicking the Open an Existing Document button. This option is here mostly because Interface Builder presents the new document assistant whenever you start Interface Builder without opening a document, or when you close all of the document windows, thus providing a quick method to either create a new nib document or open an existing one. Adding a New Nib Document to a Project When you save a newly created nib document for the fi rst time, Interface Builder looks to see whether it is saved inside the project folder of any project currently open in Xcode. If it is, Interface Builder offers to add it to that project through the dialog shown in Figure 13 - 10. Select the targets you want the nib document to be included in and click the Add button. If you plan to localize your nib document, follow these steps: 1. Create the new nib document for the development (that is, your) localization. FIGURE 13-10 c13.indd 242c13.indd 242 1/21/10 3:36:33 PM1/21/10 3:36:33 PM Download at getcoolebook.com 2. Save the fi le in the project folder and let Interface Builder add it to the project. 3. Close the nib document in Interface Builder. 4. Localize the nib document in Xcode as described in the “ Localizing Files ” section of Chapter 6. TIP TO REMEMBER If your project already has .lproj folders (because other fi les have been localized), you can save your new fi le directly into the .lproj folder of the appropriate language. Xcode treats the nib document as if it has already been localized. You can skip the step of localizing the fi le, and jump right to adding new localizations. OPENING SISTER LOCALIZATIONS In addition to the obvious ways of opening a nib document — opening it from Xcode or the Finder, the File ➪ Open command, or the File ➪ Open Recent menu — Interface Builder has a shortcut for opening other localizations of the same fi le. The File ➪ Open Localization menu lists all of the other language variants of the currently active nib document. Interface Builder detects when a nib document is located in an .lproj localization folder. It then fi nds all sister .lproj folders containing the same document and adds them to this submenu. This allows you to quickly switch between localizations of the same fi le. The Open All command opens all localizations at once. CREATING OBJECTS The fi rst “ C ” of Interface Builder is creating objects. Objects are easy to create: drag an object from the library palette into your nib document or view window. Creating an object is as easy as dragging an instance out of the library item and dropping it into your nib document window, as shown in the middle in Figure 13 - 11. You can also drop a view object directly into a view window, as shown on the left in Figure 13 - 11 — as long as it ’ s an object appropriate for that view. Creating Objects ❘ 243 c13.indd 243c13.indd 243 1/21/10 3:36:40 PM1/21/10 3:36:40 PM Download at getcoolebook.com . view controller class in Xcode also adds its associated nib document to the project for you. FIGURE 13- 7 FIGURE 13- 8 c 13. indd 239 c 13. indd 239 1/21/10 3: 36 :32 PM1/21/10 3: 36 :32 PM Download at getcoolebook.com 240 ❘ . 13- 10 c 13. indd 242c 13. indd 242 1/21/10 3: 36 :33 PM1/21/10 3: 36 :33 PM Download at getcoolebook.com 2. Save the fi le in the project folder and let Interface Builder add it to the project. 3. Close. window. c 13. indd 241c 13. indd 241 1/21/10 3: 36 :33 PM1/21/10 3: 36 :33 PM Download at getcoolebook.com 242 ❘ CHAPTER 13 INTERFACE BUILDER Interface Builder still supports legacy Carbon nib documents,

Ngày đăng: 04/07/2014, 06:20

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan