Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Objective-C.Programming.pdf
Скачиваний:
14
Добавлен:
21.02.2016
Размер:
8.64 Mб
Скачать

Chapter 27 Your First iOS Application

When a Cocoa Touch application quits or is sent to the background, it sends its delegate a message from the UIApplicationDelegate protocol so that the delegate can take care of business and respond to these events gracefully. In BNRAppDelegate.m, fill in the stubs of these two application delegate callbacks to save the to-do list:

- (void)applicationDidEnterBackground:(UIApplication *)application

{

//This method is only called in iOS 4.0+

//Save our tasks array to disk

[tasks writeToFile:docPath() atomically:YES];

}

- (void)applicationWillTerminate:(UIApplication *)application

{

//This method is only called in iOS versions prior to 4.0

//Save our tasks array to disk

[tasks writeToFile:docPath() atomically:YES];

}

Now build and run your completed application. This exercise was intended to give you a taste of iOS development. There’s much, much more out there to do and learn.

For the More Curious: What about main()?

When you began learning C and Objective-C, you learned that the entry point into your program’s code is the main() function. It’s absolutely true in Cocoa / Cocoa Touch development as well, although it’s extremely rare to edit this function in Cocoa and Cocoa Touch applications. Open main.m, and you’ll see why:

return UIApplicationMain(argc, argv, nil, NSStringFromClass([BNRAppDelegate class]));

Well, that was anti-climactic. Only one line of actual code.

The UIApplicationMain() function creates the necessary objects for your application to run. First, it creates a single instance of the UIApplication class. Then, it creates an instance of whatever class is denoted by the fourth and final argument and sets it to be the application’s delegate, so that it can send its delegate messages when memory gets low, when the application is quit or backgrounded, or when it finishes launching.

And that’s the trail from main() to application:didFinishLaunchingWithOptions: and your custom code.

190

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

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 File New New 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.

191

Chapter 28 Your First Cocoa Application

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

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.

#import <Cocoa/Cocoa.h>

@interface BNRDocument : NSDocument <NSTableViewDataSource>

{

NSMutableArray *todoItems;

IBOutlet NSTableView *itemTableView;

}

- (IBAction)createNewItem:(id)sender;

@end

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

192

A look at Interface Builder

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.”

A look at Interface Builder

In the project navigator, find and select a file named BNRDocument.xib. When you select a file in the project navigator that ends in .xib (XML Interface Builder document), Interface Builder appears in the editor pane, displaying a layout grid.

Right now, there is only one view object on the layout grid – a window object. That’s an instance of NSWindow, to which you’ll add other view objects shortly.

Now, in the upper-right corner of the Xcode window, click on the righthand View button to reveal the Utilities. At the top of Utilities, click the button to reveal the inspector.

The inspector is subdivided into separate inspectors. In this chapter, you’ll use the attributes, size, and connections inspectors.

Figure 28.3 Attributes, size, and connections inspectors

At the bottom of the Utilities pane, below the inspector, is the library, which is also divided into tabs. Select the button to reveal the object library. This library presents a list of all of the different object

193

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]