Cover by Alasdair Allan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

O'Reilly logo

Edit Mode

So far, so good. But it would be nice if we could add more cities to our guide and, if we’re not interested in a particular city, delete it as well. Let’s implement a first cut at that using the UITableViewController edit mode. You’ll have seen this many times when using iPhone applications such as the Mail application. There is an Edit button on the top right on the navigation bar. When tapped, it will drop the table view into edit mode, allowing you to delete mail messages. In some applications, the Edit button lets you add entries to the table view.

This is such a commonly implemented pattern that there are hooks inside the UIViewController to simplify things. In the viewDidLoad: method of RootController.m, you need to add the following line of code:

self.navigationItem.rightBarButtonItem = self.editButtonItem;

This will add an Edit button to the navigation bar. Clicking on this button calls a method called setEditing:animated: on the view controller, which sets the table view into edit mode and changes the Edit button to a Done button. Clicking on the Done button will take the table view out of edit mode, and calls the setEditing:animated: method again, although this time to different effect (ending the edits and changing the button back to an Edit button).

Since we want to be able to add new cities, when the table view is put into editing mode we’re going to add another cell to our table view prompting us to “Add New City...”. When this is clicked, we’ll open a new view ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required