1. Trang chủ
  2. » Công Nghệ Thông Tin

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

8 153 0

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 8
Dung lượng 2,7 MB

Nội dung

262 ❘ CHAPTER 13 INTERFACE BUILDER The identity inspector has also become a dumping ground for obscure attributes that don ’ t fi t neatly into any of the other inspectors. Depending on the nib document architecture and object type, you might fi nd an accessibility attributes group, a tool tips group, a help system identity group, or a dynamically defi ned object attributes group in the inspector. If you ’ re trying to fi nd an attribute that you think should be editable in Interface Builder, but can ’ t fi nd it, poke around in the identity inspector; it might be there. Inspecting Multiple Objects Generally, the inspector palette displays whatever attributes are common to all of the objects in a multi - object selection. Thus, if all of the selected objects are button objects, the attributes inspector presents all of the attributes for a button object. Changing any of the attributes in the inspector updates that value in every button. If the selection is heterogeneous, the inspector presents the most detail common to all of the objects. Selecting a button and a box view object allows you to edit the properties common to both: whether the views are hidden, their position, resize behavior, and so on, but it would not, for example, allow you to set whether the objects were enabled, because the enabled property is an NSControl attribute and NSBox is not a subclass of NSControl. CONNECTING OBJECTS Connections are probably Interface Builder ’ s defi ning feature. To restate, a connection is little more than an object reference: one object has an instance variable that points to another object. Interface Builder allows you to set that variable to point to another object simply by dragging a connection line from one to the other. There are two principal kinds of connections in Interface Builder: Outlets are simple instance variable references from one object to another. An outlet gives one object direct access to another object. Outlets allow you to weave arbitrary graphs of objects in a nib document. Actions are a Cocoa - specifi c duo consisting of an object reference (an outlet) and a message identifi er (an Objective - C selector). Taken together, they defi ne the object that will receive the message and exactly what message it will receive. Actions are sent almost exclusively by view objects in response to some user - initiated action, such as a mouse click, key press, touch gesture, and so on. Actions are how you “ wire ” your interface objects to your controller objects that will ultimately do the work. Interface Builder outlets and actions are standard features of hundreds of preexisting classes in the Cocoa and Cocoa Touch frameworks. It ’ s like having a huge collection of Lego blocks, ready to be connected together to form whatever you can imagine. In addition, you can add outlet properties and action methods to your own classes and then connect them to other objects exactly the same way. See the section “ Custom Classes ” for a complete description of creating and connecting your own objects in Interface Builder. In the sections that follow, the term “this object” means “the currently selected Interface Builder object, which is the subject of the active inspector palette.” ➤ ➤ c13.indd 262c13.indd 262 1/21/10 3:37:38 PM1/21/10 3:37:38 PM Download at getcoolebook.com Interface Builder provides no fewer than fi ve different techniques for setting connections — evidence of how central connections are to the Interface Builder paradigm. I introduce them one at a time in the new few sections. All connections are the same. The different techniques are simply progressively more sophisticated shortcuts. Learn the basic ones fi rst, and then explore the shortcuts as you spend more time making Interface Builder connections. Connections Inspector The connections inspector, introduced earlier, shows the outlets and actions sent by the object. Figure 13 - 30 shows the connections inspector for three different kinds of objects. FIGURE 13-30 The Outlets group is where the outlets of the object are viewed, set, and broken. The Sent Actions group (Cocoa) or Events group (Cocoa Touch) lists the kinds of events that will trigger an action. Cocoa control objects only have one kind of event, so the list is the object that will receive the action and the action message sent. Cocoa Touch objects have many different kinds of events that cause an action message to be sent. Each one can be individually connected with a recipient/message pair. All of these groups contains connections “ from ” this object; in other words, object references maintained by this object that refer to other objects. The Received Actions and Referencing Outlets groups are the complementary perspective to the Outlets, Sent Actions, and Events groups. Interface Builder thoughtfully cross - references all of the outlet references and sent actions confi gured from any other object in your nib document, and lists those references in these two groups. The Referencing Outlets group shows you all the other objects that have outlets connected to this object. The Received Actions group lists all of the action methods that this object defi nes, and shows you which objects are confi gured to send them. Some events, and the Received Actions and Referencing Outlets groups, may have multiple connections for a single outlet or action. These will be collected together in the single connection. Use the disclosure triangle to expose multiple connections and manipulate them individually. Connecting Objects ❘ 263 c13.indd 263c13.indd 263 1/21/10 3:37:44 PM1/21/10 3:37:44 PM Download at getcoolebook.com 264 ❘ CHAPTER 13 INTERFACE BUILDER Don ’ t confuse connections with containers. A container object has implicit references to the objects it contains. A connection is an explicit reference, identifi ed by name, and deliberately set between two objects. Connecting an Outlet The simplest and most basic way to set an outlet connection is from the connections inspector of the object that defi nes the outlet. To set a connection, click the connection circle next to the outlet in the Outlets group and drag it to the object you want it connected to. You can choose any representation of the target object you can fi nd in Interface Builder; you can drag it to the object in the document window (as shown), or its visual representation in a view window. The left side of Figure 13 - 31 shows how the docMenu outlet of the NSApplication object is being connected to an NSMenu object in the nib document. FIGURE 13-31 After releasing the mouse button, the connection is established, as shown on the right of Figure 13 - 31. The object outlet shows the name of the object to which it is connected. When the nib document is loaded at run time, the NSApplication ’ s docMenu outlet property will point to a new instance of NSMenu. That ’ s all there is to making outlet connections! Interface Builder only allows connections to objects of the correct class. Interface Builder knows the class of each object, the action methods it implements, and the class of each outlet property. Interface Builder only permits connections to objects that are members of the outlet’s class, and only creates action connections with messages that are implemented by the receiver. If an outlet or action doesn’t appear in a list, or you think Interface Builder is ignoring your request to create a connection, it’s most likely that you’re trying to create an invalid connection. c13.indd 264c13.indd 264 1/21/10 3:37:44 PM1/21/10 3:37:44 PM Download at getcoolebook.com Exploring and Breaking Connections Hovering the cursor over a connection highlights the object it is connected to, also shown on the right in Figure 13 - 31. To clear a connection, click the close (x) button next to the connection. You can also overwrite a connection by simply connecting the outlet to a different object. Unconnected outlets are ignored when the nib document is loaded, which typically means they will contain a nil value. Connecting Another Object to This Object The Referencing Outlets group contains a special New Referencing Outlet connection. It ’ s an abstract outlet that lets you create connections to this object by setting an outlet in another object. In Figure 13 - 31, the inspector palette was focused on the NSApplication object that defi nes the docMenu outlet. What if the inspector was focused on the NSMenu object instead? While you could switch the selection to the NSApplication object and then drag its docMenu outlet back to the NSMenu object, the New Referencing Outlet connection provides a more effi cient route. The left side of Figure 13 - 32 shows the New Referencing Outlet being dragged from the NSMenu object to the NSApplication object. FIGURE 13-32 When the mouse button is released, Interface Builder presents a menu of all possible outlets that could refer to this object, as shown in the middle of Figure 13 - 32. Clicking one of the outlets sets the outlet in the object you dragged to , to this object. After being set, the connection appears in this object ’ s Referencing Outlets group. The result is identical to having set the outlet from the NSApplication object, but you weren ’ t forced to fi rst change your object focus. If you want to set that same outlet in another object, drag the connection circle next to the new referencing connection to that object. This time, Interface Builder does not prompt you to choose which outlet; the outlet will always be the same as the one previously set. Remember the distinction between the two: Creating a New Referencing Connection lets you choose which outlet to set. Creating a referencing connection from an existing referencing connection will set the same outlet in the chosen object that is set in the other objects. ➤ ➤ Connecting Objects ❘ 265 c13.indd 265c13.indd 265 1/21/10 3:37:50 PM1/21/10 3:37:50 PM Download at getcoolebook.com 266 ❘ CHAPTER 13 INTERFACE BUILDER Creating a new referencing connection will not replace any other connections to this object, so new connections will accumulate in the group — although, you might be overwriting outlets that were previously connected in some other object. Connecting an Action Creating an action connection is only slightly more complicated than creating an outlet connection, owing to the fact that it involves two pieces of information: the receiving object and the message to send. You create an action connection almost exactly as you would an outlet connection. In the sent actions or events group, drag a connection circle to the receiving object, as shown in the left of Figure 13 - 33. FIGURE 13-33 When you release the mouse button, a menu appears listing all of the action methods defi ned by the receiver. Select the action message you want to send and the connection is complete. The fi nished connection, shown on the right of Figure 13 - 33, displays both the name of the receiving object and the message it will receive. Action connections are explored and broken exactly the same way as outlet connections. Connecting an Action to This Object Just as with the Referencing Objects group, an object ’ s Received Actions group lists the other objects that have actions connected to this one. Unlike the Referencing Objects group, the list of actions is fi xed; it ’ s the action methods implemented by this object. An object can only receive action messages that it has implemented. (Sending a message that isn ’ t implemented is a run time error.) It works pretty much the same way the Referencing Objects group does. To confi gure an object to send an action to this object, select the connection circle next to the action that you want this object to receive, and than drag it to the object that you want to send it. If the sender is a Cocoa object, you ’ re done. If it ’ s a Cocoa Touch object that generates multiple events, you must now select which event you want to trigger that message, as shown in Figure 13 - 34. c13.indd 266c13.indd 266 1/21/10 3:37:51 PM1/21/10 3:37:51 PM Download at getcoolebook.com Pop - Up Connections Panel Technically, you ’ ve learned everything you need to know to create, explore, dissolve, and cross - reference connections in your nib document, but creating connections is such a common activity that Interface Builder provides a number of additional shortcuts. In the earlier techniques you set connections by: 1. Selecting an object. 2. Selecting the connections inspector panel. 3. Dragging the desired outlet or action to the target object. Pop - up connection panels, shown in Figure 13 - 35, eliminate step 2 of that procedure. Pop - up connection panels are fl oating panels that appear over an object when you Right/Control - click an object. Click the close button or press the Esc key to dismiss the panel. A pop - up connections panel is identical to the connections inspector for that object — the only difference is that you didn ’ t have to open the connections inspector. All of the controls work exactly the way they do in the connections inspector, with a couple of added features. FIGURE 13-34 FIGURE 13-35 Connecting Objects ❘ 267 c13.indd 267c13.indd 267 1/21/10 3:37:52 PM1/21/10 3:37:52 PM Download at getcoolebook.com 268 ❘ CHAPTER 13 INTERFACE BUILDER The fi rst is the “ connect behind ” feature. If you hover the cursor over the panel as you begin to make a connection, the panel will fade away, as shown in Figure 13 - 36. This allows you to make connections to objects obscured by the pop - up connections panel. The second feature appears if the object you ’ ve chosen is in an object hierarchy. Figure 13 - 37 shows a table view object. The table is contained within a scroll view and a window, and contains a column view (among others). FIGURE 13-36 FIGURE 13-37 The pop - up connection panel for the table includes a small set of arrow buttons at the right edge of the panel ’ s title. Clicking those arrows, as shown in Figure 13 - 37, brings up a menu of the objects in the hierarchy at the coordinate that you originally clicked. This is the same sequence of objects that you would get by “ drilling down ” at that point in the interface. Selecting one of the other objects in the menu refocuses the pop - up connections panel to that object, allowing you Right - click one object and then set a connection for one of its containers or subobjects. Quick Connection A quick connection is Interface Builder shorthand. It creates a connection between two objects with a single gesture. To create a connection (outlet or action) between any two objects: 1. Right/Control - click the source object. 2. Hold down the mouse button and drag to the destination object, as shown in Figure 13 - 38. 3. Release the mouse button. 4. Select the outlet or action to connect. The direction of the drag is the same that you use in the Outlets, Sent Actions, and Events groups: Drag from the object with the outlet to the object you want the outlet connected to. Drag from the object that sends an action to the object that implements the action. ➤ ➤ c13.indd 268c13.indd 268 1/21/10 3:37:52 PM1/21/10 3:37:52 PM Download at getcoolebook.com When you release the mouse button, a menu appears with every outlet and action connection that ’ s possible between those two objects. Select the outlet or action and the connection is complete. FIGURE 13-38 There ’ s a limitation when making quick connections from Cocoa Touch objects. When you create an action connection, the action is always connected to the Touch Up Inside event. In other words, you can ’ t use a quick connection to select both the event to connect to and the action message to send. Use the pop - up connections panel if you need to connect an action to some event other than Touch Up Inside. The real power of connections and actions emerges when you defi ne your own outlets and action messages. This is explained in the next section. CUSTOM CLASSES Interface Builder would only be mildly useful if all it could do was create, confi gure, and connect instances of the predefi ned framework objects found in the library palette. The power of Interface Builder explodes when you can defi ne your own classes, outlets, and actions, and then use Interface Builder to create instances of your custom objects, confi gure them, and then connect them just like any other nib document object. Using Interface Builder you can: Add instances of your custom classes to a nib document. Defi ne outlets in your custom classes that will be recognized by Interface Builder. Defi ne action methods in your custom classes that will be recognized by Interface Builder. Interface Builder recognizes when a nib document belongs to an open Xcode project. It then interfaces with Xcode to extract information about your class defi nitions and incorporate that into the document. It does this quickly, quietly, and transparently in the background. All you have to do is defi ne your class. By the time you ’ ve saved your class fi les and switched to Interface Builder, your class defi nitions will have been assimilated and are ready to instantiate, confi gure, and connect. ➤ ➤ ➤ Custom Classes ❘ 269 c13.indd 269c13.indd 269 1/21/10 3:37:53 PM1/21/10 3:37:53 PM Download at getcoolebook.com . features. FIGURE 13- 34 FIGURE 13- 35 Connecting Objects ❘ 267 c 13. indd 267c 13. indd 267 1/21/10 3: 37:52 PM1/21/10 3: 37:52 PM Download at getcoolebook.com 268 ❘ CHAPTER 13 INTERFACE BUILDER The. connection. Use the disclosure triangle to expose multiple connections and manipulate them individually. Connecting Objects ❘ 2 63 c 13. indd 263c 13. indd 2 63 1/21/10 3: 37:44 PM1/21/10 3: 37:44 PM Download. which event you want to trigger that message, as shown in Figure 13 - 34 . c 13. indd 266c 13. indd 266 1/21/10 3: 37:51 PM1/21/10 3: 37:51 PM Download at getcoolebook.com Pop - Up Connections Panel

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

TỪ KHÓA LIÊN QUAN