01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Building the PocketTasks application<br />

249<br />

In viewDidLoad you add a button to add new tasks. The configureCell:atIndex-<br />

Path: method configures a table cell to display the name of the task and to show a<br />

checkmark if the task is done. The addTask method adds a new task and sets its name<br />

to Task 1, Task 2, and so on. Feel free to add a user interface that lets the user enter<br />

a real name for a task. Notice that the person relationship of the task in this method<br />

is set to make the new task belong to the selected person. Also notice the use of<br />

[NSNumber numberWithBool:NO]. Why can’t you just say [task setValue:NO forKey:<br />

@"isDone"]? Because you always have to provide an object for the value, never a primitive<br />

value. That’s why you have to box an integer, double, float, or Boolean value in an<br />

instance of NSNumber.<br />

Finally, the tableView:didSelectRowAtIndexPath: method marks a task as done<br />

by setting the value, saving the context, and calling configureCell:atIndexPath: to<br />

display the checkmark.<br />

To make your new TasksViewController appear when you tap on a person, you<br />

have to go back to PeopleViewController.m once more. Using the code in listing 12.15,<br />

import TasksViewController.h and implement the tableView:didSelectRowAtIndex-<br />

Path: method to create an instance of TasksViewController and push it onto the<br />

navigation controller stack.<br />

Listing 12.15<br />

tableView:didSelectRowAtIndexPath: in PeopleViewController.m<br />

- (void)tableView:(UITableView *)tableView<br />

didSelectRowAtIndexPath:(NSIndexPath *)indexPath {<br />

}<br />

TasksViewController *tasksVC =<br />

[[TasksViewController alloc]<br />

initWithPerson:[resultsController objectAtIndexPath:indexPath]];<br />

[self.navigationController pushViewController:tasksVC animated:YES];<br />

[tasksVC release];<br />

Now build and run the application. You should be able to add tasks and set them to<br />

done by tapping on them (see figure 12.5).<br />

12.4.9 Using model objects<br />

So far you’ve been using NSManagedObject to access your people and tasks. While this<br />

works just fine, it would be much nicer to be able to say [person firstName] (or<br />

person.firstName, the dot notation for properties) instead of [person valueFor-<br />

Key:@"firstName"]. You also need the full name of a person in several places in your<br />

application. Rather than putting the first and last names together every time you need<br />

the full name, you can more efficiently just say [person fullName]. Core Data provides<br />

an easy way to do so using model object classes, which are custom subclasses of<br />

NSManagedObject. You can add your own methods to model object classes and have<br />

nice accessor methods for all the properties and relationships.<br />

To convert PocketTasks to use model object classes, you first let Xcode generate<br />

the appropriate classes for you. Select File > New > New File…, and then select

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!