Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a right bar button to a navigation bar in iphone

I want to add a right bar button item to the navigation bar, so that on click, it performs certain a function.

I have created the following code to add the right bar button item, but after it is done, the bar button item is not getting displayed in navigation bar:

-(void)viewDidload{     self.navigationItem.rightBarButtonItem =      [[[UIBarButtonItem alloc]             initWithBarButtonSystemItem:UIBarButtonSystemItemAdd                                                                                                                                      target:self                                 action:@selector(Add:)] autorelease];     }  -(IBAction)Add:(id)sender {     TAddNewJourney *j=[[TAddNewJourney alloc]init];     [app.navigationController pushViewController:j animated:YES];     [j release]; } 
like image 314
Rani Avatar asked Apr 29 '11 12:04

Rani


People also ask

Can you change Iphone navigation bar?

A user changes the navigation bar's style, or UIBarStyle , by tapping the “Style” button to the left of the main page. This button opens an action sheet where users can change the background's appearance to default, black-opaque, or black- translucent.

How do I customize my navigation bar?

From Settings, tap Display, and then tap Navigation bar. Make sure Buttons is selected, and then you can choose your desired button setup at the bottom of the screen. Note: This option will also affect the location you swipe when using Swipe gestures.


2 Answers

Let assume, you have a UIViewController, like this:

UIViewController *vc = [UIViewController new]; 

And you added it to navigation controller:

UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc]; 

In this way rightBarButtonItem will NOT getting displayed:

nc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"xyz" style:UIBarButtonItemStyleDone target:self action:@selector(xyz)]; 

BUT this way IT WILL appear:

vc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"xyz" style:UIBarButtonItemStyleDone target:self action:@selector(xyz)]; 

Use your own viewcontroller instead of the navigationcontroller to refer navigationItem.

like image 111
János Avatar answered Sep 23 '22 02:09

János


-(void)viewDidLoad {      [super viewDidLoad];     app.navigationController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(Add:)] autorelease]; }  -(IBAction)Add:(id)sender {     TAddNewJourney *j=[[TAddNewJourney alloc]init];     [app.navigationController pushViewController:j animated:YES];     [j release]; } 

Try the other answers. I posted this answer so that it will work if your viewcontroller does not have a navigation controller which i think is the problem.

like image 25
visakh7 Avatar answered Sep 27 '22 02:09

visakh7