3

I'm facing some problems in implementing a tableview, with "Back", "Edit" and "Add" buttons on the navigation bar. The tableview is reached by clicking on a row of another tableview, so the "Back" button is added automatically. With the storyboard I've added the "Add" button to the navigation bar. With code I've added the "Edit" button (I used code, since if I add the button with the storyboard, I don't know how to reproduce the "Edit" standard behavior...):

self.navigationItem.leftBarButtonItem = self.editButtonItem;

The problem is that, in this way, the "Edit" button hides the "Back" button on the navigation bar.

At this point, I've two questions:

  1. Is it possible with storyboard to add a third button on the navigation bar?
  2. In case I've to do this programmatically, I know that I can do this as follows:

    UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
    [button setFrame:CGRectMake(width-90,6,50,30)];
    [button setTitle:@"Edit" forState:UIControlStateNormal];
    button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    [self.navigationController.navigationBar addSubview:button];
    

But how can I implement via code the standard behavior of the "Edit" button? I mean, I click "Edit" and the button becomes "Done" and the rows become deletable...

Thanks in advance, yassa

yassassin
  • 3,185
  • 6
  • 28
  • 31

2 Answers2

4

Incase anyone else should happen to stumble onto this question as well the solution is pretty easy. UINavigationItem has a property for rightItems wich is just an array of UIBarButtonItems. Put both an add button and an edit button into an array and assign it to rightItems and your done :-) And here is an example code snippet:

UITableViewController *table = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self     action:@selector(insertNewObject:)];
NSArray *barButtons = [NSArray arrayWithObjects:table.editButtonItem, addButton, nil];
table.navigationItem.rightBarButtonItems = barButtons;
CJPresler
  • 63
  • 3
  • This is really the better answer – Wayne Dec 09 '13 at 23:50
  • In swift: `let addButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "methodName:"); self.navigationItem.rightBarButtonItems = [self.editButtonItem(), addButton]` – Kevin Qi Jul 12 '15 at 22:43
2

First, the Apple docs say 'you do not add subviews to a navigation bar directly'. Don't know if this is enough to get the app bounced from the store, but it's not considered "proper".

Second, you can add more than three buttons to a UINavigationItem in iOS 5 but not in iOS 4 or earlier.

Finally, I'd leave the edit button top right and back top left. That's where people expect them. If I wanted an add button (and are on iOS 5), I'd place it next to the edit button.

Sorry; no help on storyboards. Don't know anything about them.

smparkes
  • 13,807
  • 4
  • 36
  • 61
  • Thanks a lot for the answer. But to implement the bar with 3 buttons programmatically, do I have to create a new toolbar: `UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 105, 44.01)];` or can I simply add another button to my Navigationbar `[self navigationItem]`? If yes, in which way? – yassassin Dec 11 '11 at 15:14
  • You just add one or more buttons to your navigation item. See the properties and methods with _BarButton_ in the name in UINavigationItem (http://developer.apple.com/library/IOs/#documentation/UIKit/Reference/UINavigationItem_Class/Reference/UINavigationItem.html). Generally I override the `navigationItem` method in my view controllers, call `super` to get the default, add the buttons if I haven't already added them, and return the result. – smparkes Dec 11 '11 at 16:46
  • Thank you very much, you opened to me an entire new world! I was able to implement a marvelous navigation bar with three buttons!!! :) – yassassin Dec 11 '11 at 17:06