Online Book Reader

Home Category

Objective-C Programming_ The Big Nerd Ranch Guide - Aaron Hillegass [64]

By Root 448 0
Until another control becomes the first responder or the current control is sent the resignFirstResponder message, that control will keep this status and receive keyboard and shake input.

When a text input control (like a text field) becomes the first responder, the keyboard materializes on the screen. As long as the current first responder is a control that accepts text input, the keyboard will remain on the screen. At the end of addTask:, we tell the text field to resign its status, which causes the keyboard to dematerialize.

Build and run the application. Now you can add tasks!

Saving task data

There is one final feature that you’ll add to iTahDoodle. Naturally, when users quit the app, they’d like their to-do lists to stick around for later.

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.

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

Return Main Page Previous Page Next Page

®Online Book Reader