28
Your First Cocoa Application

In this chapter, you are going to create TahDoodle, a desktop Cocoa application. Like iTahDoodle, TahDoodle is a simple to-do list application that stores its data as a property list; however, there are some differences. In the iOS application, you used instances of UITableView, UITextField, and UIButton. In this desktop application, you will place the task list in an NSTableView where it can be edited directly. You will also have an NSButton that will insert a new row in the table view where you can add a new task.

Figure 28.1  Complete TahDoodle application

Complete TahDoodle application

In addition, in the last chapter, you built your user interface programmatically. In this chapter, you will use a tool included in Xcode called Interface Builder to create, configure, and connect the elements of your user interface.

In Xcode, choose FileNewNew Project.... Under the Mac OS X section, click on Application. From the template choices that appear, select Cocoa Application and name the project TahDoodle. TahDoodle is document-based, which means the user can have multiple to-do lists open simultaneously. Document Extension refers to the filename extension to be used when saving your documents (to-do lists) to disk. Set the extension for your data files to be tdl. TahDoodle will not use Core Data or need unit tests.

Figure 28.2  Creating a new Cocoa Application

Creating a new Cocoa Application

Edit BNRDocument.h

Open BNRDocument.h and add a method and two instance variables: todoItems will be a mutable array of strings and itemTableView will be a pointer to the NSTableView object that will display the strings in todoItems. Also, declare that BNRDocument conforms to the NSTableViewDataSource protocol.

#​i​m​p​o​r​t​ ​<​C​o​c​o​a​/​C​o​c​o​a​.​h​>​

@​i​n​t​e​r​f​a​c​e​ ​B​N​R​D​o​c​u​m​e​n​t​ ​:​ ​N​S​D​o​c​u​m​e​n​t​ ​<​N​S​T​a​b​l​e​V​i​e​w​D​a​t​a​S​o​u​r​c​e​>​
{​
 ​ ​ ​ ​N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​*​t​o​d​o​I​t​e​m​s​;​
 ​ ​ ​ ​I​B​O​u​t​l​e​t​ ​N​S​T​a​b​l​e​V​i​e​w​ ​*​i​t​e​m​T​a​b​l​e​V​i​e​w​;​
}​
-​ ​(​I​B​A​c​t​i​o​n​)​c​r​e​a​t​e​N​e​w​I​t​e​m​:​(​i​d​)​s​e​n​d​e​r​;​

@​e​n​d​

Note that there is no instance variable for the Insert button. (You’ll see why later.) There is, however, an action for the button – the createNewItem: method.

In the last chapter, the target of the button’s action was the instance of the application delegate class, BNRAppDelegate. A document-based application doesn’t have an application delegate object and instead is built around a subclass of NSDocument. For TahDoodle, that class is BNRDocument.

In a document-based application, the user can have multiple instances of document objects open at the same time. So when TahDoodle is running, you can have multiple instances of BNRDocument (multiple to-do lists). Each instance will have its own table view, button, tasks array, and window. Each instance will respond to messages independently of the others, and each instance will be its own button’s target.

In the declarations you entered, there are also two new terms: IBOutlet and IBAction. These tell Xcode This is a pointer (IBOutlet) or an action method (IBAction) that the developer will use Interface Builder to connect rather than doing so programmatically.

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

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