Making connections

Creating and configuring views is not all that you can do in Interface Builder. You can also connect, or wire, these view objects to your application’s code. In particular, you can set target-action pairs and assign pointers.

To the left of the layout grid is a list of placeholders and objects. This is called the document outline. (If you just see a stack of icons without labels, click the Making connections button to reveal the expanded list.)

Figure 28.10  Minimized and expanded document outline

Minimized and expanded document outline

Find File's Owner under the Placeholders section of the document outline. A placeholder stands in for a particular object that cannot be specified until runtime. File's Owner stands in for whichever object will load this XIB as its user interface. In your case, File's Owner represents an instance of BNRDocument.

Now select the Insert button in the editor and Control-drag to File's Owner.

Figure 28.11  Making connections

Making connections

When you release the mouse button, a small pop-up will appear showing the available connections that you can make. Choose createNewItem: as the action to be triggered by the button.

Figure 28.12  Selecting an action

Selecting an action

You’ve now configured the target-action pair. This is equivalent to the following code in iTahDoodle.

 ​ ​ ​ ​[​i​n​s​e​r​t​B​u​t​t​o​n​ ​a​d​d​T​a​r​g​e​t​:​s​e​l​f​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​a​c​t​i​o​n​:​@​s​e​l​e​c​t​o​r​(​a​d​d​T​a​s​k​:​)​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​f​o​r​C​o​n​t​r​o​l​E​v​e​n​t​s​:​U​I​C​o​n​t​r​o​l​E​v​e​n​t​T​o​u​c​h​U​p​I​n​s​i​d​e​]​;​

Next, connect BNRDocument’s itemTableView outlet. Control-drag from File's Owner (standing in for the BNRDocument) to the NSTableView and release. Choose the only option that appears for this outlet: itemTableView.

Figure 28.13  Making more connections

Making more connections

Finally, Control-Shift-click on the table view and select the NSTableView from the list. Then Control-drag from the table view to File's Owner and select dataSource from the connection pop-up.

Figure 28.14  Connecting the table view’s data source

Connecting the table view’s data source

So what, exactly, did you just do? You assigned pointers. Your BNRDocument class declares a pointer to an NSTableView. You instructed Interface Builder that you want this pointer to point to the specific NSTableView instance that you dragged onto the window. Similarly, the table view has a few pointers of its own, like dataSource. You instructed Interface Builder to point the table view’s dataSource pointer at your BNRDocument. This is equivalent to when you set the table view’s data source programmatically in iTahDoodle.

[​t​a​s​k​T​a​b​l​e​ ​s​e​t​D​a​t​a​S​o​u​r​c​e​:​s​e​l​f​]​;​

When you entered the IBOutlet and IBAction keywords, you were flagging those outlets and actions for Interface Builder, saying Hey! When I try to connect a pointer in IB, make sure and put this item in the list of available connections! As you write your code, Interface Builder scans for IBOutlet and IBAction keywords so that it knows what connections you might want to make.

Here are the actual definitions:

#​d​e​f​i​n​e​ ​I​B​A​c​t​i​o​n​ ​v​o​i​d​
#​d​e​f​i​n​e​ ​I​B​O​u​t​l​e​t​

From what we learned about #define, we know that IBAction is replaced with void before the compiler sees it and that IBOutlet disappears altogether. Thus, at compile time, all IBOutlet keywords are removed entirely (leaving behind the outlets themselves). IBAction keywords are replaced by void because actions invoked by user interface controls are not expected to have a return value.

Note that you don’t allocate XIB-based user interface elements as you do when creating your interface programmatically. They are automatically allocated when the XIB is loaded at runtime.

Finally, recall that there is no pointer to the button. This is because an object only needs instance variables for objects it needs to send messages to. The button needs to send messages to the BNRDocument instance, which is why we wired its action. The BNRDocument, however, does not need to send messages to the button, and so it does not need a pointer to it.

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

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