R – Can you set a navbar’s edit button in Interface Builder

cocoa-touchinterface-builder

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?

Best Answer

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.