Objects are easy to create: drag an object from the library palette into your nib document or view window.. Here are a few shortcuts and tricks for creating objects in Interface Builder:
Trang 12. 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
Trang 2Using either technique, subview objects
can be inserted directly into the view
hierarchy When dropping into the nib
document window, drop the object inside
the appropriate view When dropping a new
object in a view window, it ’ s implied that the
new object will be inserted into a container
view, even if that container is just the
window object Interface Builder highlights
and identifi es the container that will receive
the new object Figure 13 - 12 illustrates
how the location of the drop determines
which superview will receive the new object
In all cases, containers only accept certain object classes (or their subclasses) For example,
you cannot add an arbitrary object (such as NSObject) inside a view container, no matter where you
attempt to drop it
The library palette will only create objects in its repertoire See the section “ Custom Classes ” later
in this chapter to learn how to create objects with custom classes
Here are a few shortcuts and tricks for creating objects in Interface Builder:
Create a new window by dragging a window object out of the library palette and dropping
it anywhere on the screen
➤
FIGURE 13-11
FIGURE 13-12
Trang 3To create an unattached view (NSView, UIView, and so on) object, drag a new view object into the nib document window to create a top - level object Double - click the new object to open it in a view window where you can edit its content
You cannot create a top - level Cocoa application menu bar object using the NSMenu object
in the library palette (It will create a menu bar, but it won ’ t be the application menu bar.)
Should you ever need to create one, use the Cocoa Main Menu template to create a new nib document that contains a full application menu bar object, copy it into your nib document, and then edit it as desired
COPYING, DUPLICATING, AND DELETING OBJECTS
Another way of creating an object that should not be overlooked is to duplicate an existing object
This can save a lot of time when creating groups of similarly confi gured objects The following actions create copies or delete objects in your nib document:
Edit ➪ Cut Edit ➪ Copy Edit ➪ Paste Edit ➪ Delete Delete key Edit ➪ Duplicate Option+drag Drag an object from one nib document to another Holding down the Option key while dragging objects, or selecting the Edit ➪ Duplicate command, duplicates the selected objects, inserting the copies wherever they are dropped or making them siblings of their originals, respectively
If you repeatedly use complex objects or layouts in your projects, you can save preconfi gured objects
in the library palette See the section “ Saving Custom Objects ” toward the end of this chapter
SELECTING OBJECTS
Before you can examine or change the attributes of an object, you must select it This sounds simple — and it usually is — but there is some subtlety to selecting objects in Interface Builder of which you ’ ll want to be aware
There are two places to select Interface Builder objects All objects appear in the nib document window and can be selected there View objects can optionally appear in a view window, approximately as they will appear in your application Selecting a view object is equivalent to selecting the same object in the nib document window
➤
➤
➤
➤
➤
➤
➤
➤
➤
➤
Trang 4Objects in the Nib Document Window
Selecting objects in the nib document window is simple and straightforward: select the object
or objects with which you want to work using any of the three view modes The icon view only
allows you to select top - level objects, and the browser mode only lets you select multiple objects
within the same branch of the hierarchy I tend to use the list view most of the time All of the
standard Mac OS X icon/list selection gestures work here: Click, Shift+click, Command+click,
and dragging out a rectangle
Selecting a container object does not select any of the objects that it contains If you want to edit the attributes of a view and all of its subviews simultaneously, you must explicitly select both the container view and its subview objects.
The advantage of selecting objects in the nib document window is that they are unambiguous The
nib document window lists each and every object defi ned in that document Of course, the nib
document window is the only place you can select objects that don ’ t have a visual representation
The disadvantage is that it may sometimes be diffi cult to distinguish between different objects in the
list For example, a nib document that defi nes fi ve untitled radio - button objects simply shows fi ve
identical radio - button icons, whereas in a view window, each radio button object appears as a radio
button, at the position it will be in that window See the “ Identity ” section to learn how to name
objects so they are easier to fi nd in the nib document window
Object Order
The order in which subobjects appear in the nib document window list defi nes their order in the nib
document Often the order of objects is completely immaterial Within a view container, the order
defi nes the Z - order of the subviews — the order in which the objects overlap The fi rst object in the list
will be behind all of the objects that come after it You can reorder objects in the document window by
dragging them to a new position in the list, or you can use any of these commands on view objects:
Layout ➪ Send to Front (Option+Shift+Command+F) Layout ➪ Send to Back
Layout ➪ Send Forward Layout ➪ Send Backwards Object order is different from object position (see the “ Moving and Resizing Objects ” section)
View Objects
View objects in the nib document window may also appear as “ themselves ” — an approximate
representation of that class of view object at run time — in a separate view window For example,
a view window containing the menu of your application lets you see, select, and edit the contents
of your menus, more or less as the menu items will appear in your application A single NSView or
UIView object will appear in a window where you can resize it, add subviews, and customize its
appearance If the view object is, itself, a window object, the view window represents that window
➤
➤
➤
➤
Trang 5View objects do not have to appear in a view window Any view window can be closed, tidying up your workspace To reveal it again, double - click the view object in the nib document window The view object, or the view window that contains it, will open again This will also select the view object in the view window Unless the view window is representative of an actual window object
in the nib document, the view window is just a convenient container provided by Interface Builder
For example, the position of the view window that contains a menubar object is superfl uous
Selecting Multiple View Objects
Select a single view object by clicking it You can select multiple view objects by dragging out a selection rectangle
Any view objects whose position intersects the selection rectangle will be selected, as shown in Figure 13 - 13
When single - clicking an object, the selection can be modifi ed using one of the following keyboard modifi ers:
Holding down the Shift key adds the object to the set of currently selected objects
Holding down the Command key toggles the selection of the object: an unselected object is selected (same as Shift), and a selected object is unselected
Selecting Interior View Objects
Some objects, like tab and split pane views, are containers for other view objects To select a container object, click any unoccupied region within the container To select an object within the container, click directly over that object Figure 13 - 14 shows two different selections: on the left, the button object contained in the box object is selected; on the right, the box object itself is selected
➤
➤
FIGURE 13-14
FIGURE 13-13
Sometimes, selecting a container is inconvenient or impossible (a split view with an invisible divider might not have any unoccupied region on which to click) Normally when you click a view object, Interface Builder selects the most specifi c object in the visual hierarchy at the clicked coordinate
This is the so - called “ inside out ” selection strategy
Holding down the Command key while clicking reverses this logic Interface Builder selects the outermost container that occupies the clicked coordinate In the example shown in Figure 13 - 14,
Trang 6clicking the Can Get Smarter radio button selects that NSButton object Holding down the Command
key while clicking same button selects its enclosing NSBox object
Drilling Into View Objects
Tables and many other kinds of nested views support a selection technique called drilling The fi rst
time you click the object, you select the container object Single - click again, and the selection “ drills
down ” into the object hierarchy
Using the table view object in Figure 13 - 15 as an example, imagine positioning your cursor over
the middle Text Cell object Clicking four times slowly — remember these are all single - click
actions — produces the following selections:
1. The fi rst click selects the scroll view object
2. The second click selects the table view object nested inside the scroll view
3. The third click selects the second table column object of the table view
4. The fourth click selects the text cell object used to draw the cells of the second column
Trang 7The same technique works with the column header objects of the table Most view objects that form
a similar hierarchy can be drilled into, such as any scroll view Of particular note is the cell object nested inside each control view object It ’ s not always obvious, but every control view (button, slider, and so on) is actually two objects: a controller object and a cell object that provides its look and feel
Object Selection vs Object Focus
There ’ s a subtle difference between selecting
a view object and having a view object that ’ s the current focus of Interface Builder Click once to select a container object Click again
to make it the focus Figure 13 - 16 shows the difference On the left, the Box view object is selected On the right, the box view has the current focus
For many commands, there ’ s no difference between these two states, but for some, notably Edit ➪ Paste, there ’ s a big difference
If there were two button objects on the clipboard, choosing the Edit ➪ Paste command while the box object was selected (left) would just paste two buttons into the window If the box object is the focus (right), the Paste command would insert the two buttons inside, making them subviews of the box object
Adjusting the Selection
A number of commands change the selection When selecting and unselecting multiple objects, don ’ t neglect these perennial favorites:
Edit ➪ Select All (Command+A) Edit ➪ Select None (Shift+Command+D) The following four commands let you shift the selection to a different object relative to the currently selected object, or objects:
Tools ➪ Select Parent (Control+Command+ ↑) Tools ➪ Select Child (Control+Command+ ↓) Tools ➪ Select Previous Sibling (Control+Command+ ← ) Tools ➪ Select Next Sibling (Control+Command+ → ) The Tools ➪ Select Parent command selects the container object of the currently selected object or objects Tools ➪ Select Child selects the fi rst object contained within the currently selected object Taken together, these two commands allow you to either “ walk up ” or “ drill down ” into the object hierarchy
The Next and Previous Sibling commands shift the selection to the next, or previous, object in the same container object These commands navigate by object order (Z - order), not position
➤
➤
➤
➤
➤
➤
FIGURE 13-16
Trang 8CONFIGURING OBJECTS
Objects in Interface Builder wouldn ’ t be much use if you couldn ’ t bend and shape them to your
needs Most of your time in Interface Builder will be spent editing objects, and you have many
different ways to do that The overt aspects (visible text, placement, and size) of view objects can
be manipulated directly: drag an object around to reposition it; double - click the text of an object
to change it
Less obvious attributes, such as formatting options and bindings, are manipulated in the
inspector palette A few specialized commands also exist for aligning objects and grouping
objects in containers
Moving and Resizing Objects
An object, or a group of selected objects, can be
repositioned in a view window simply by dragging
it to another location, as shown in Figure 13 - 17
Depending on the layout, some objects may not be
repositioned For instance, an object in one half of a
split view always occupies that entire space It cannot
be repositioned, because its location and size are
determined by the split view Altering the position of
the split view or its divider is the only way to change
that object ’ s position in the window
Interface Builder displays guidelines as an object is
dragged When the object is released, it “ snaps ” to
nearest guideline See the “ Guides ” section to fi nd out how to control these automatic guidelines or
create your own
The arrow keys nudge the selected objects one pixel in any direction Hold down the Shift key to
nudge them 10 pixels at a time
Dragging the handles on the edge of an object resizes it Some objects cannot be resized, or
can only be resized in certain directions For example, the height of a button is fi xed in Cocoa
You can only change its width Using the resize handles, you can only resize one object at a time
TIP TO REMEMBER
Holding down the Command and Options keys while resizing a container view, such as a window or box view, resizes its subviews too The subviews are resized according to their resize behavior This is the same effect that you get when resizing
a container view using the Size inspector Both the Size inspector and resize behavior are described later in the “Size” section
FIGURE 13-17