1

Hi I have a right navigation bar button item I created in storyboard. I would now like to add a second right button as in Apple's calendar app. The pure code version would be:

navigationItem.rightBarButtonItems = [rightA, rightB]

However, I have already created one button in storyboard with various outlets and segues and would ideally like to preserve it while adding the second. My understanding is you can now create both buttons in storyboard in Xcode7 but I am still in Xcode 6 so am thinking of some sort of hybrid storyboard code solution.

Is this possible in Xcode 6?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user1904273
  • 4,562
  • 11
  • 45
  • 96

2 Answers2

4

Just create an array with the existing item and the new item:

navigationItem.rightBarButtonItems = [navigationItem.rightBarButtonItem!, rightB]

(or the other way round, as you prefer).

pbasdf
  • 21,386
  • 4
  • 43
  • 75
  • does this preserve the storyboard item? Also what is the significance of the exclamation point ! – user1904273 Jan 03 '16 at 22:16
  • @user1904273 Yes, it preserves the existing bar button item from the storyboard. The ! is because it's a Swift optional. Technically I should check whether it is nil, but for demo purposes, I used ! to unwrap. If you use Objective-C, the code is nearly identical: remove the !, but use `@[...]` in place of `[...]` to create the array. – pbasdf Jan 03 '16 at 23:14
  • How do you grab name of bar button created in storyboard? It is not recognizing navigationItem.rightBarButtonItem. Thx! – user1904273 Jan 04 '16 at 14:08
0

Suppose you have a navigationController in which you would like to have multiple buttons. Since iOS 5 you can assign an array. The problem is that you lose all the benefits of using the storyboard as it will be done programmatically.

I used the following trick. Usually when you want multiple button on the navigation bar you don't want a toolbar.

In the current view (not in the navigation controller) where you want the buttons to appear, show the toolbar by changing

bottomBar = inferred to bottomBar = toolbar.

enter image description here

A toolbar will appear at the bottom. Add UIBarButtons to this bar. Link them to other view controllers using segues, etc ... in your .h file create an outlet for each button

@property (strong, nonatomic) IBOutlet UIBarButtonItem *Button1;

@property (strong, nonatomic) IBOutlet UIBarButtonItem *Button2;

@property (strong, nonatomic) IBOutlet UIBarButtonItem *Button3;

self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:self.Button3, self.Button2, self.Button1, nil];

self.navigationController.toolbarHidden = YES;

Akash
  • 461
  • 2
  • 14