Can't Assign Multiple Buttons to Uinavigationitem When Using Storyboard with iOS 5

Can't assign multiple Buttons to UINavigationItem when using Storyboard with iOS 5

EDIT

At the time I answered this question, Xcode was not offering the possibility of linking added buttons in the storyboard. The trick presented permitted to still have the segues designed in the storyboard.

With more recent versions of Xcode, for sure the solution introduced by @ecotax and later the more detailed answer of @ShimanskiArtem are the ones to be used.


I had the same problem as you and I found the following trick

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.

Sample Image

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;

Then in your viewDidLoad() link the buttons to the navigation bar and hide the toolbar. Add them in the reverse order of the order you want to see them

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

self.navigationController.toolbarHidden = YES;

And voilà you have multiple buttons in your navigation bar

Sample Image

Sample Image

and the result in the simulator

Sample Image

Sample Image

UIBarButton placed in toolbar instead of UINavigationBar

If anyone faces this problem, I did the following:

I couldn't add a Bar button because there were no top bar in that view. First I tried to put a Navigation Bar in the view, but Xcode crashed. Then I tried to put a Navigation Item, which worked. After that I could place my bar button in the top bar.

What I don't understand is why I could put the Add (+) button in the previous view, since that doesn't have any navigation entry either, but I'm guessing it's since that view was the root view controller of a Navigation Controller. Someone else can maybe give a more detailed answer.

Multiple buttons in iOS Navigation Bar via Interface Builder?

Update: Xcode 7 add multi buttons support.

Just drag bar button items to navigation bar, you may need add a navigation item first if the view controller don't have one.

demo


You can drag a UIView to the navigation bar, then add two UIButton to that view.

Cannot add two UIBarButtonItem througn interface builder directly. But you can make it through code. Like [UINavigationItem setRightBarButtonItems:animated:].

Can you add buttons to navigation bars through storyboard?

You can just drag out a Bar Button Item and drop it on the right end of the view controller's navigation bar:

Sample Image

How to add a right button to a UINavigationController?

Try doing it in viewDidLoad. Generally you should defer anything you can until that point anyway, when a UIViewController is inited it still might be quite a while before it displays, no point in doing work early and tying up memory.

- (void)viewDidLoad {
[super viewDidLoad];

UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)];
self.navigationItem.rightBarButtonItem = anotherButton;
// exclude the following in ARC projects...
[anotherButton release];
}

As to why it isn't working currently, I can't say with 100% certainty without seeing more code, but a lot of stuff happens between init and the view loading, and you may be doing something that causes the navigationItem to reset in between.

xcode add a button to my storyboard navigation item?

I don't know how to do this using the storyboard but it can be done in the code as follows.

In the viewDidLoad method of completionVC do the following:

  1. create UIBarButtonItem for community
  2. create UIBarButtonItem for add
  3. Add the buttons to the navigationItem using:

    self.navigationItem.rightBarButtonItems = [NSArray arrayWithItems:community, add, nil];

Add more then one button in top navigation bar in iOS

UINavigationItem Class Reference having property of leftBarButtonItems and rightBarButtonItems so you can add multiple UIBarButtonItem in your navigation control using following example code:

 UIBarButtonItem *FirstButton = [[UIBarButtonItem alloc] 
initWithTitle:@"First"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(AcionFirst:)];

UIBarButtonItem *secondButton = [[UIBarButtonItem alloc]
initWithTitle:@"Second"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(AcionSecond:)];


self.navigationItem.leftBarButtonItems= [NSArray arrayWithObjects:FirstButton,secondButton,nil];


-(void)AcionFirst:(id)sender
{
//do something for first bar button

}

-(void)AcionSecond:(id)sender
{
//do something for second bar button
}


Related Topics



Leave a reply



Submit