The next piece of functionality is making the list of Attractions editable. Not the individual attractions, but the list itself- adding, deleting, and reordering the list. But before we do that, let’s take a step back, as I haven’t talked about the application delegate yet.
Every iOS application is based on a class called UIApplicationMain, provided by Cocoa Touch itself. UIApplicationMain handles the setup and tear down of your application, the main event window, and other application-level tasks. In other development environments, you might think to subclass UIApplicationMain to provide the custom functionality that is your application. iOS uses a different idiom however, that of the application delegate.
For those familar with Windows development, the application delegate class provides callback functions for the application object. There are methods for when the application terminates, enters the background, re-enters the foreground, and so forth. But the most important method is called when the application starts
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launch options
Before I started to make the list editable, this routine did two things. First, it created the AttractionsViewController object, which is the view controller for my list of attractions. Second, it added the AttractionsViewController as the root controller of the application, and started the whole engine up:
AttractionsViewController *attractionsController = [[AttractionsViewController alloc] init]; [[self window] setRootViewController:attractionsController];
Now let’s talk about making the list editable. My first change is to wrap AttractionsViewController in a navigation controller. The UINavigationController class provides the stuff necessary to navigate a hierarchical set of data. In also provides a handy toolbar. So the code above changes to
AttractionsViewController *attractionsController = [[AttractionsViewController alloc] init]; UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:attractionsController]; [[self window] setRootViewController:navController];
Enough about the application delegate, let’s go back to the AttractionsViewController itself and its init: method.
The first change here is to set up that toolbar I told you about. You’ll see why in a minute.
[[self navigationItem] setTitle:@"Fort Collins for Kids"];
Easy enough, gives us a title.
[[self navigationItem] setLeftBarButtonItem:[self editButtonItem]];
Adds an edit button to the left side of the toolbar. This automatically puts the table view list into edit mode.
UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNewAttraction:)]; [[self navigationItem] setRightBarButtonItem:bbi];
This is just a little more complicated. It sets up the right button to create new rows in the table view. The UIBarButtonSystemItemAdd gives the button a + sign, and the action parameter specifies the method that is called on the AttractionsViewController when the button is clicked. Here is what it all looks like when we’re done:
Clicking that edit button puts the UITableView into editing mode, which probably looks familiar to iOS users:
Moving rows, adding rows, and deleting rows in the UITableView list are enabled by implementing one routine for each. I’ll go into the details of these in my next post.