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

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


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.


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


Add this code in viewdidload

UIBarButtonItem *chkmanuaaly = [[UIBarButtonItem alloc]initWithTitle:@"Calculate" style:UIBarButtonItemStylePlain target:self action:@selector(nextview:)];self.navigationItem.rightBarButtonItem = chkmanuaaly;