views:

1608

answers:

2

It's easy enough to set up a table view to allow editing. Just add one line to your UITableViewController:

self.navigationItem.rightBarButtonItem = self.editButtonItem;

This line adds an edit button on the nav bar that will change the table to editing mode and change its button text to "Done" while editing.

Is it possible to set this up in Interface Builder? I see that you can add a UIBarButtonItem and can set its "Identifier" to "Edit", but I don't see the expected behavior.

BTW, what does the "Identifier" in the Attributes panel do?

+1  A: 

Yes, you can add UIBarButtonItems in Interface Builder, and they should work.

The identifier lets you use a preset button (like Edit or Reload), or you can choose Custom and make your own button.

EDIT: I may be able to help further if you could explain how UIBarButtonItems added through IB don't work.

UPDATE: UIViewController.editButtonItem is a special method that returns a UIBarButtonItem that invokes the view's setEditing method. You can achieve the same effect by creating a method that does the same thing and connecting the selector to your UIBarButtonItem in IB.

In your header file:

- IBAction edit:(id)sender;

and in your implementation file:

- (IBAction) edit:(id)sender {
 [self setEditing:YES animated:YES];
}

then connect the selector to the UIBarButtonItem.

However, you might not be able to create this connection in the default Navigation-Based Application template since the Table View is in a separate file.

Can Berk Güder
- New project: Navigation-Based Application.- RootViewController.m: uncomment viewDidLoad and the self.nav... line - Run. There's no table data, but click Edit to see that it changesCan you recomment the line and get the behavior by changes to MainWindow.xib? Or is it only set programmatically?
DrGary
A: 

Have a look here: http://blog.tmro.net/2009/05/uitabbarbuttonitem-did-not-change-its.html

If you want your button to be able to change its label dynamically make sure you use a custom identifier otherwise its title will be immutable.

nicktmro