Graphics Programs Reference
In-Depth Information
[n setTitle:@"Homepwner"];
// Create a new bar button item that will send
// addNewItem: to ItemsViewController
UIBarButtonItem *bbi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(addNewItem:)];
// Set this bar button item as the right item in the navigationItem
[[self navigationItem] setRightBarButtonItem:bbi];
[[self navigationItem] setLeftBarButtonItem:[self editButtonItem]];
}
return self;
}
Surprisingly, that's all the code you need to get an edit button on the navigation bar. Build
and run, tap the
Edit
button, and watch the
UITableView
enter editing mode! Where
does
editButtonItem
come from?
UIViewController
has an
editBut-
tonItem
property, and when sent
editButtonItem
, the view controller creates a
UIBarButtonItem
with the title
Edit
. Even better, this button comes with a target-ac-
tion pair: it sends the message
setEditing:animated:
to its
UIViewControl-
ler
when tapped.
Now that
Homepwner
has a fully functional navigation bar, you can get rid of the header
view and the associated code. In
ItemsViewController.m
, delete the following
methods.
- (UIView *)tableView:(UITableView *)aTableView
viewForHeaderInSection:(NSInteger)section
{
return [self headerView];
}
- (CGFloat)tableView:(UITableView *)tableView
heightForHeaderInSection:(NSInteger)section
{
return [[self headerView] frame].size.height;
}
- (UIView *)headerView
{
if (!headerView) {
[[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:self options:nil];
}
return headerView;
}
- (void)toggleEditingMode:(id)sender
{
if ([self isEditing]) {
[sender setTitle:@"Edit" forState:UIControlStateNormal];