Online Book Reader

Home Category

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

By Root 442 0
windowFrame = [[UIScreen mainScreen] bounds];

UIWindow *theWindow = [[UIWindow alloc] initWithFrame:windowFrame];

[self setWindow:theWindow];

// Define the frame rectangles of the three UI elements

// CGRectMake() creates a CGRect from (x, y, width, height)

CGRect tableFrame = CGRectMake(0, 80, 320, 380);

CGRect fieldFrame = CGRectMake(20, 40, 200, 31);

CGRect buttonFrame = CGRectMake(228, 40, 72, 31);

// Create and configure the table view

taskTable = [[UITableView alloc] initWithFrame:tableFrame

style:UITableViewStylePlain];

[taskTable setSeparatorStyle:UITableViewCellSeparatorStyleNone];

// Create and configure the text field where new tasks will be typed

taskField = [[UITextField alloc] initWithFrame:fieldFrame];

[taskField setBorderStyle:UITextBorderStyleRoundedRect];

[taskField setPlaceholder:@"Type a task, tap Insert"];

// Create and configure a rounded rect Insert button

insertButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[insertButton setFrame:buttonFrame];

// Buttons behave using a target/action callback

// Configure the Insert button's action to call this object's -addTask: method

[insertButton addTarget:self

action:@selector(addTask:)

forControlEvents:UIControlEventTouchUpInside];

// Give the button a title

[insertButton setTitle:@"Insert"

forState:UIControlStateNormal];

// Add our three UI elements to the window

[[self window] addSubview:taskTable];

[[self window] addSubview:taskField];

[[self window] addSubview:insertButton];

// Finalize the window and put it on the screen

[[self window] setBackgroundColor:[UIColor whiteColor]];

[[self window] makeKeyAndVisible];

return YES;

}

Running on the iOS simulator

Now that you’ve set up your views, we can build the application to see how they look. In Xcode, find the Scheme dropdown menu near the Run button. Select iPhone 5.X Simulator for the latest version of the iOS simulator:

Figure 27.6 Scheme selector

Build and run the application. You’ll get a warning from the compiler that you that you haven’t implemented addTask:. Ignore that for now; you’ll implement addTask: shortly.

The simulator lets you run Cocoa Touch applications on your desktop. This is a quick and easy way to see what your program should look and act like when it runs on an iOS device.

You can see the views that you set up and laid out in application:didFinishLaunchingWithOptions:, but they can’t do anything yet. In fact, tapping the Insert button will crash the application because the button’s action method, addTask:, isn’t implemented yet. (That’s one of the reasons the compiler warned you about it.)

Figure 27.7 iTahDoodle object diagram

Wiring up the table view


You’ve got a table view on the screen, but it has no clue about what it should display. As a view object, the table view does not contain anything about actual data. It needs an object to act as its data source. In iTahDoodle, your table view’s data source will be the instance of BNRAppDelegate.

In BNRAppDelegate.m, update application:didFinishLaunchingWithOptions: to send a message to the table view that makes the BNRAppDelegate instance its data source:

...

// Create and configure the table view

taskTable = [[UITableView alloc] initWithFrame:tableFrame

style:UITableViewStylePlain];

[taskTable setSeparatorStyle:UITableViewCellSeparatorStyleNone];

// Make this object the table view's dataSource

[taskTable setDataSource:self];

// Create and configure the text field where new tasks will be typed

taskField = [[UITextField alloc] initWithFrame:fieldFrame];

...

In order for the table view’s data source to do its job, it must implement methods in the UITableViewDataSource protocol. First, update BNRAppDelegate.h to declare that BNRAppDelegate conforms to this protocol:

@interface BNRAppDelegate : UIResponder

{

UITableView *taskTable;

UITextField *taskField;

UIButton *insertButton;

NSMutableArray *tasks;

}

- (void)addTask:(id)sender;

The UITableViewDataSource protocol

Return Main Page Previous Page Next Page

®Online Book Reader