Online Book Reader

Home Category

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

By Root 436 0
denoted by the MVC pattern. Most of the Cocoa and Cocoa Touch APIs are written with MVC in mind, and your own code should be, too. Now let’s return to our controller, the instance of BNRAppDelegate.

The application delegate


When an iOS application first launches, there is a lot of behind-the-scenes setting up. During this phase, an instance of UIApplication is created to control your application’s state and act as liaison to the operating system. An instance of BNRAppDelegate is also created and set as the delegate of the UIApplication instance (which explains the name “app delegate”).

While the application is being launched, it is not ready for work or input. When this changes, the UIApplication instance sends its delegate the message application:didFinishLaunchingWithOptions:. This method is very important. It’s where you put everything that needs to happen or needs to be in place before the user interacts with the application.

In iTahDoodle, one of the things we need to do in this method is find the property list and load it into an array. In BNRAppDelegate.m, notice that there is already a stub for the application:didFinishLaunchingWithOptions: method. Find it and replace the code between its braces with the following:

#pragma mark - Application delegate callbacks

- (BOOL)application:(UIApplication *)application

didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

// Attempt to load an existing to-do dataset from an array stored to disk.

NSArray *plist = [NSArray arrayWithContentsOfFile:docPath()];

if (plist) {

// If there was a dataset available, copy it into our instance variable.

tasks = [plist mutableCopy];

} else {

// Otherwise, just create an empty one to get us started.

tasks = [[NSMutableArray alloc] init];

}

}

Wondering about the #pragma mark at the start of this code? Objective-C programmers often use this construct to group their methods within a class. Xcode knows about it, too. On the navigation bar at the top of the editor, find the item to the right of BNRAppDelegate.m. (Right now, this item probably reads @implementation AppDelegate, but it depends on where your cursor is in the code.) Click on this item, and Xcode will show you a list of locations in this file. You can click on any of these to be taken to that location in the code. Notice that your new pragma mark shows up on this list. This is very handy when you have many methods in a class.

Setting up views


Another thing we need to do before the application is ready for action is set up our view objects. This includes creating them, configuring them, and putting them on the screen. Makes sense, right? The user can’t tap a button that doesn’t exist or is not on the screen.

In iTahDoodle, you’re going to set up your views programmatically in application:didFinishLaunchingWithOptions:. There is also a visual “drag-and-drop” tool for setting up views that we’ll use in the next chapter.

I should warn you that here’s where the code starts getting dense. The detailed syntax of creating and showing views on the screen is a topic for a book specifically about iOS application programming. Try to follow the gist of what’s going on as you type in the code. You create each object and then configure it by setting some of its properties. Next, the configured view objects are added as subviews of the window object, and, finally, the window is placed on the screen.

#pragma mark - Application delegate callbacks

- (BOOL)application:(UIApplication *)application

didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

// Attempt to load an existing to-do dataset from an array stored to disk.

NSArray *plist = [NSArray arrayWithContentsOfFile:docPath()];

if (plist) {

// If there was a dataset available, copy it into our instance variable.

tasks = [plist mutableCopy];

} else {

// Otherwise, just create an empty one to get us started.

tasks = [[NSMutableArray alloc] init];

}

// Create and configure the UIWindow instance

// A CGRect is a struct with an origin (x,y) and size (width,height)

CGRect

Return Main Page Previous Page Next Page

®Online Book Reader