Making connections

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

File’s Owner

In BNRDocument.xib, find the Placeholders heading in the document outline. A placeholder stands in for a particular object that cannot be specified until runtime.

One of these placeholders is File's Owner. File's Owner stands in for the object that will load the XIB file as its user interface. In your case, it represents the instance of BNRDocument.

Setting the button’s target-action pair

Recall from the object diagram at the beginning of the chapter (Figure 32.2) that the NSButton has a target-action pair: its target is the instance of BNRDocument, and its action is addTask:.

For iTahDoodle, you configured the target-action pair for the button in BNRAppDelegate.m:

-​ ​(​B​O​O​L​)​a​p​p​l​i​c​a​t​i​o​n​:​(​U​I​A​p​p​l​i​c​a​t​i​o​n​ ​*​)​a​p​p​l​i​c​a​t​i​o​n​
d​i​d​F​i​n​i​s​h​L​a​u​n​c​h​i​n​g​W​i​t​h​O​p​t​i​o​n​s​:​(​N​S​D​i​c​t​i​o​n​a​r​y​ ​*​)​l​a​u​n​c​h​O​p​t​i​o​n​s​
{​
 ​ ​ ​ ​.​.​.​

 ​ ​ ​ ​[​s​e​l​f​.​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​]​;​
 ​ ​ ​ ​.​.​.​
}​

For TahDoodle, you are going to make this connection in Interface Builder.

In the layout grid, select the Add Task button. While pressing and holding the Control key, drag from the button to File's Owner in the document outline.

Figure 32.13  Making a connection

Making a connection

Release the mouse button, and a list of methods will appear. Select addTask:.

Figure 32.14  Selecting an action

Selecting an action

When you Control-dragged from the button to File's Owner, you set BNRDocument as the target of the button. When you selected addTask: from the list, you set this method as the action of the button.

Note that addTask: was only available to choose from the list of methods because you included the IBAction keyword in its declaration in BNRDocument.h:

-​ ​(​I​B​A​c​t​i​o​n​)​a​d​d​T​a​s​k​:​(​i​d​)​s​e​n​d​e​r​;​

IBAction is a flag for Interface Builder that says Hey! When I try to connect a target-action pair in IB, make sure to include this method in the list of possible actions.

Here is the actual definition of IBAction:

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

Remember what you learned about #define in Chapter 25? This statement tells you that IBAction is replaced with void before the compiler sees it. All IBAction keywords can replaced with void because actions invoked by user interface controls are not expected to have a return value.

To run and test this connection, you must implement addTask:. In the project navigator, select BNRDocument.m. You are now out of Interface Builder and back in your familiar code editor.

In BNRDocument.m, first add a pragma mark to group the existing methods in BNRDocument:

#​i​m​p​o​r​t​ ​"​B​N​R​D​o​c​u​m​e​n​t​.​h​"​

@​i​m​p​l​e​m​e​n​t​a​t​i​o​n​ ​B​N​R​D​o​c​u​m​e​n​t​

#​p​r​a​g​m​a​ ​m​a​r​k​ ​-​ ​N​S​D​o​c​u​m​e​n​t​ ​O​v​e​r​r​i​d​e​s​

.​.​.​

@​e​n​d​

Then implement addTask: as a stub that logs a message to the console:

#​i​m​p​o​r​t​ ​"​B​N​R​D​o​c​u​m​e​n​t​.​h​"​

@​i​m​p​l​e​m​e​n​t​a​t​i​o​n​ ​B​N​R​D​o​c​u​m​e​n​t​

#​p​r​a​g​m​a​ ​m​a​r​k​ ​-​ ​N​S​D​o​c​u​m​e​n​t​ ​O​v​e​r​r​i​d​e​s​

.​.​.​

#​ ​p​r​a​g​m​a​ ​m​a​r​k​ ​-​ ​A​c​t​i​o​n​s-​ ​(​v​o​i​d​)​a​d​d​T​a​s​k​:​(​i​d​)​s​e​n​d​e​r{​
 ​ ​ ​ ​N​S​L​o​g​(​@​"​A​d​d​ ​T​a​s​k​ ​b​u​t​t​o​n​ ​c​l​i​c​k​e​d​!​"​)​;}​

@​e​n​d​

Build and run the application. Click the Add Task button and confirm that your target-action pair is working as expected.

Connecting the table view

Earlier in the chapter, you declared the taskTable pointer in BNRDocument.h:

@​p​r​o​p​e​r​t​y​ ​(​n​o​n​a​t​o​m​i​c​)​ ​I​B​O​u​t​l​e​t​ ​N​S​T​a​b​l​e​V​i​e​w​ ​*​t​a​s​k​T​a​b​l​e​;​

You want to assign the NSTableView object in BNRDocument.xib to this pointer.

Reopen BNRDocument.xib. Control-drag from File's Owner (standing in for the BNRDocument) to the table view in the layout grid. When you release the mouse button, choose taskTable from the list of connections.

Figure 32.15  Making more connections

Making more connections

Now the taskTable pointer that you declared in BNRDocument.h points to the specific instance of NSTableView defined in BNRDocument.xib.

Outlet is just another word for object pointer. Recall that you included IBOutlet when declaring the taskTable pointer:

@​p​r​o​p​e​r​t​y​ ​(​n​o​n​a​t​o​m​i​c​)​ ​I​B​O​u​t​l​e​t​ ​N​S​T​a​b​l​e​V​i​e​w​ ​*​t​a​s​k​T​a​b​l​e​;​

Unlike IBAction, IBOutlet is defined so that it will disappear completely before the compiler sees it:

#​d​e​f​i​n​e​ ​I​B​O​u​t​l​e​t​

Thus, at compile time, all IBOutlet keywords get removed, leaving behind the outlets (pointers) themselves.

NSTableView has a few pointers of its own, including dataSource. You want the instance of BNRDocument to be assigned to the table view’s dataSource pointer.

In the layout grid, Control-Shift-click on the table view to select the NSTableView. Then Control-drag from the table view to File's Owner. When you release the mouse button, choose dataSource from the list of connections.

Figure 32.16  Connecting the table view’s data source

Connecting the table view’s data source

This accomplishes the same thing that you did in iTahDoodle with the following code:

-​ ​(​B​O​O​L​)​a​p​p​l​i​c​a​t​i​o​n​:​(​U​I​A​p​p​l​i​c​a​t​i​o​n​ ​*​)​a​p​p​l​i​c​a​t​i​o​n​
d​i​d​F​i​n​i​s​h​L​a​u​n​c​h​i​n​g​W​i​t​h​O​p​t​i​o​n​s​:​(​N​S​D​i​c​t​i​o​n​a​r​y​ ​*​)​l​a​u​n​c​h​O​p​t​i​o​n​s​
{​
 ​ ​ ​ ​.​.​.​

 ​ ​ ​ ​s​e​l​f​.​t​a​s​k​T​a​b​l​e​.​d​a​t​a​S​o​u​r​c​e​ ​=​ ​s​e​l​f​;​

 ​ ​ ​ ​.​.​.​
}​
..................Content has been hidden....................

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