user1017932
user1017932

Reputation: 77

multiple rightbar buttons in navigationbar in xcode4.2

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.?

Upvotes: 0

Views: 1678

Answers (2)

akashivskyy
akashivskyy

Reputation: 45210

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];

Upvotes: 1

The iOSDev
The iOSDev

Reputation: 5267

Please add UINavigationBar instead of UINavigationItem

Upvotes: 0

Related Questions