I would like to have two rightBarButtonItems on navigation bar. One for Edit and the other for Add.
Obviously I can't make it using Interface Builder.
Does anybody know how to do it programmatically? Thanks!
I would like to have two rightBarButtonItems on navigation bar. One for Edit and the other for Add.
Obviously I can't make it using Interface Builder.
Does anybody know how to do it programmatically? Thanks!
You can't have more than one rightBarButtonItem. However, you can provide instead of a textual title, a titleView:
@property(nonatomic, retain) UIView *titleView
this is a property of the UINavigationItem class. Thus, you may for instance, add a segmented control as titleView. This way, you may add immediately even more than two buttons. Otherwise, simply design you own view adding the buttons you need,and taking into account that its dimensions will be constrained by the navigation bar where you are going to insert it.
the navigation bar is a UIView so you can simply create a regulat UIButton and add it to your navigation bar as a subView.
Set the frame relative to the nav bar. If you want it to look exactly like the built in button, you will probably have to produce the graphics yourself as they are not exposed to the SDK AFAIK.
My suggestion would be to not implement the Add functionality as a button in the navigation bar. I assume that you're dealing with a table view of items below, so one way of handling this user interaction is to display an "Add new item" option as the last entry in your table view. This could be programmatically faded in when the user taps on the Edit button in your navigation bar by implementing the following delegate method:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[self.tableView beginUpdates];
[self.tableView setEditing:editing animated:YES];
if (editing)
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[objects count] inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[objects count] inSection:0];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
[self.tableView endUpdates];
}
You then would need to make sure that the extra row is accounted for by increasing the count of rows using the following:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView.editing)
return ([objects count] + 1);
else
return [objects count];
}
and then showing the green plus sign to its left, as opposed to the normal deletion editing style:
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.editing == NO || !indexPath) return UITableViewCellEditingStyleNone;
if (indexPath.row >= [objects count])
return UITableViewCellEditingStyleInsert;
else
return UITableViewCellEditingStyleDelete;
return UITableViewCellEditingStyleNone;
}
Of course, you'll need to supply a name for it in your cellForRowAtIndexPath: implementation and handle its row selection, as well.
Heres an example of how you can add two buttons as the rightbarbutton. The code below creates a segmentedcontrol containing up and down arrow buttons, which is then added as a customview for navigation's rightbarbutton:
UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray array]];
[segmentedControl setMomentary:YES];
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"icon-triangle-up.png"] atIndex:0 animated:NO];
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"icon-triangle-down.png"] atIndex:1 animated:NO];
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segmentedControl addTarget:self action:@selector(segmentedAction:) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem * segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView: segmentedControl];
self.navigationItem.rightBarButtonItem = segmentBarItem;