3

My current application uses the storyboard interface builder, and I have reached my limit on adding bar button items to the navigation bar. I currently have a right bar button item and a left bar button item, but I need to add a third button to segue to a new view controller. Since dragging the new bar button item wouldn't work in the storyboard, I created the button programmatically within the view controller and used arrayByAddingObject to incorporate the current right bar item and the new right bar item. I would really like to keep the UI within the storyboard if at all possible.

My first question here is is there a way to view the button I created programmatically within storyboard? If not, is there a simple solution to adding more than two bar button items within storyboard? Below is the my current code for the new bar button item.

One last thing: If I choose to go with programmatic approach, how could I segue to a view controller I created in storyboard in the action method.

Button Initialized
self.categoryButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cat", @"categories button") style:UIBarButtonItemStyleDone target:self action:@selector(categoryButtonDidPress:)];
self.navigationItem.rightBarButtonItems = [self.navigationItem.rightBarButtonItems arrayByAddingObject:self.categoryButtonItem];
Action Method
-(void) categoryButtonDidPress:(UIBarButtonItem *)sender {

}
Community
  • 1
  • 1
Alex Blair
  • 584
  • 1
  • 4
  • 19
  • http://stackoverflow.com/questions/931457/more-than-1-rightbarbuttonitem-on-navigation-bar – lukeswitz Jul 07 '15 at 22:58
  • this was very useful, but adding a view to the navigation bar to 'house' the buttons removes the UIBarButtonSystem options. In other words, you can no longer use UIBarButtonItem and must use UIButton. Is there a way to display the 'search' icon within a UIButton? – Alex Blair Jul 07 '15 at 23:20

1 Answers1

2

I don't think you can see that button in the storyboard. It is ok to add it programmatically though. Just create a segue in the storyboard without attaching it to any particular button (just drag from one view controller to another, and create the segue). Give the segue an identified (in the attributes inspector), for example "CategorySegue". Then do this:

-(void) categoryButtonDidPress:(UIBarButtonItem *)sender {
    [self performSegueWithIdentifier:@"CategorySegue" sender:nil];
}
almas
  • 7,090
  • 7
  • 33
  • 49
  • This was exactly what I was looking for, and I really appreciate it. I would up vote if I could, but I'm new on the Stack Overflow scene. Many thanks! – Alex Blair Jul 08 '15 at 00:04
  • I have one more question that I've been pulling my hair out on. I need to use this new category view controller as a popover. Since the button was created programmatically, I can't set the anchor within the storyboard. Any idea on how to proceed with this? – Alex Blair Jul 08 '15 at 00:49
  • If you want a popover check out this thread: http://stackoverflow.com/questions/14770980/how-to-display-a-popover-programmatically-from-a-uibutton-that-is-also-created-p – almas Jul 08 '15 at 05:36