Online Book Reader

Home Category

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

By Root 428 0
code or project templates, visit the Big Nerd Ranch forum for this book at forums.bignerdranch.com for help.

After choosing the Empty Application template, click Next and name this project iTahDoodle. The Company Identifier and Bundle Identifier are related to keeping each app on the App Store unique. Both are strings in reverse-domain notation. Big Nerd Ranch uses com.bignerdranch as its Company Identifier.

Figure 27.3 Configuring the iTahDoodle project

Whatever you enter as the Class Prefix will be prepended to the name of the initial class that the template creates for you. This two- or three-letter prefix will keep your class names distinct from Apple’s or anyone else’s. (Use BNR for now to keep our code in sync.)

Notice that Apple’s code includes prefixes. All of Apple’s classes that you’ve seen so far begin with the prefix NS, which stands for NeXTSTEP, the platform for which the Foundation framework was originally designed. In this chapter, you’ll also use classes from the UIKit framework, and they will begin with UI.

Finally, make iTahDoodle an iPhone (as opposed to iPad or Universal) application. iTahDoodle will use Automatic Reference Counting but not use Core Data or unit tests.

BNRAppDelegate


When Xcode created your project via the Empty Application template, it created one class for you: BNRAppDelegate. Your “app delegate” is the starting point of your application, and every iOS application has one. The single instance of BNRAppDelegate is responsible for processing events and coordinating the work of other objects in the application.

Open BNRAppDelegate.h. Add four instance variables and an instance method. The first three instance variables are pointers to objects that the user can see and interact with – a table view that will display all the tasks to be done, a text field where you can enter a new task, and a button that will add the new task to the table. The fourth object is a mutable array. This is where you will store the tasks as strings.

#import

@interface BNRAppDelegate : UIResponder

{

UITableView *taskTable;

UITextField *taskField;

UIButton *insertButton;

NSMutableArray *tasks;

}

- (void)addTask:(id)sender;

@property (strong, nonatomic) UIWindow *window;

@end

Notice that the file UIKit.h was imported by the template. UIKit is the framework that contains most of the iOS-specific classes, like UITableView, UITextField, and UIButton. In addition, BNRAppDelegate conforms to the UIApplicationDelegate protocol.

Note that we don’t import Foundation.h. How then can we use NSMutableArray? In this template, the Foundation framework header is part of the precompiled header file for this project, so Foundation classes are available to use. (Don’t believe me? Click on iTahDoodle-Prefix.pch under Supporting Files in the project navigator and see for yourself.)

Adding a C helper function


We can tell from the instance variable declarations that the iTahDoodle application will include at least four additional objects. However, before we get to these objects, you’re going to write a C function. In Objective-C, we usually get things done with methods rather than functions. So when we do use a C function in an Objective-C application, we often refer to them as “helper” functions.

iTahDoodle will store the user’s tasks as a property list – an XML file. Therefore, you will need a way to get this file’s location while your application is running. You’re going to write a C function that returns that file path as an NSString.

To add a helper function to your application, you first need to declare it in BNRAppDelegate.h.

#import

// Declare a helper function that we will use to get a path

// to the location on disk where we can save the to-do list

NSString *docPath(void);

@interface BNRAppDelegate : UIResponder

{

UITableView *taskTable;

UITextField *taskField;

UIButton *insertButton;

NSMutableArray *tasks;

}

- (void)addTask:(id)sender;

@property (strong,

Return Main Page Previous Page Next Page

®Online Book Reader