Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone - Add "Add" Button when Edit button is selected in UITableView

i have a table view in my application which shows some items. when i click on one item, a new table view appears (with navigation controller: push). So at the top of the Table view there is now the navigationcontroller with the automatic "back" arrow to get back. i have the "edit" button enabled on the right side.

Now i want when i tap on the edit button, the Back button should disappear and a "+" add button should be there instead of the back button. Is this possible? Or it is possible to get the Edit and Add button on the screen at the same time?

thanks

like image 357
Mike_NotGuilty Avatar asked Nov 22 '13 18:11

Mike_NotGuilty


2 Answers

This is easy enough. Override the setEditing:animated: method of your view controller. This is called when the Edit/Done button is toggled (assuming you are using the standard editButtonItem from UIViewController).

In this method you create an "add" button and make it the left bar button item. This will hide the back button. Remove the "add" button and the back button will reappear.

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];

    if (editing) {
        // Add the + button
        UIBarButtonItem *addBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAction:)];
        self.navigationItem.leftBarButtonItem = addBtn;
    } else {
        // remove the + button
        self.navigationItem.leftBarButtonItem = nil;
    }
}
like image 134
rmaddy Avatar answered Sep 30 '22 17:09

rmaddy


Yep, this is the approach that I used:

Have a property for both the back button and the add button and set it in viewDidLoad:

self.backButton = self.navigationItem.leftBarButtonItem;
self.addButton  = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addPressed:)];

Now you just have to swap the buttons and update the TableView state accordingly when 'Edit' is pressed. Here i also change the 'Edit' button to 'Done':

- (IBAction)editBarButtonPressed:(UIBarButtonItem *)sender {
    if (self.tableView.editing == NO) {
        UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(editBarButtonPressed:)];

        self.navigationItem.rightBarButtonItem = myButton;
        [self.tableView setEditing:YES animated:YES];

        [self.navigationItem setLeftBarButtonItem:self.addButton animated:YES];
    } else {
        UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editBarButtonPressed:)];

        self.navigationItem.rightBarButtonItem = myButton;
        [self.tableView setEditing:NO animated:YES];

        [self.navigationItem setLeftBarButtonItem:self.backButton animated:NO];
    }
}

Hope this answers your question. :) br denrase

like image 41
denrase Avatar answered Sep 30 '22 17:09

denrase