0

I want to create three buttons in right side of Navigation bar.I am using storyboard for creating the UIView Controller. In DetailViewController am Embedded a Navigation bar using storyboard and then creating three UIBarButtonItem programetically and then adding these in an array then assigning it to navigationitem.This is working fine.

UIBarButtonItem *Button1 = [[UIBarButtonItem alloc]initWithTitle:@"Button1" style:UIBarButtonItemStylePlain
                                                           target:self action:@selector(Button1Clicked:)] ;

UIBarButtonItem *Button2 = [[UIBarButtonItem alloc] initWithTitle:@"Button2" style:UIBarButtonItemStylePlain
                                                             target:self action:@selector(Button2Clicked:)] ;

UIBarButtonItem *Button3 = [[UIBarButtonItem alloc] initWithTitle:@"Button3" style:UIBarButtonItemStylePlain
                                                              target:self action:@selector(Button3Clicked::)] ;

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

I have another viewcontroller this is a modalviewcontreoller. I am creating the view controller using storyboard and adding a navigationbar not a navigationcontroller.Then using the same method for adding buttons to navigtion bar, but not shown any buttons. Plese anyone know how to solve this issue.?

user1017932
  • 77
  • 2
  • 9
  • Please provide some info or code where you stuck exactly – The iOSDev Apr 14 '12 at 12:37
  • Hi thanks for the reply.When clicking on a button a new Vewcontroller is displayed i want a navigation bar with three buttons on the rightside of this viewcontroller.The above code not working on this.What am missing? – user1017932 Apr 14 '12 at 13:22

2 Answers2

1

If your viewController is in UINavigationController you can simply use

self.navigationItem ...

which works fine as I see. But if your viewController (your modal in this case) is not in UINavController, you have to access UINavigationItem in this way:

someNavigationBar.topItem ...

So, if you set an IBOutlet to your navigationBar, your code should look like this one:

UIBarButtonItem *Button1 = ...
UIBarButtonItem *Button2 = ...
UIBarButtonItem *Button3 = ...

yourNavigationBar.topItem.rightBarButtonItems = [NSArray arrayWithObjects:Button1, Button2, Button3, nil];
akashivskyy
  • 44,342
  • 16
  • 106
  • 116
0

Please add UINavigationBar instead of UINavigationItem

The iOSDev
  • 5,237
  • 7
  • 41
  • 78