Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a button to my navigationController's right side after pushing another view controller in?

So, immediately after pushing a view controller to my tableView,

// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView
        didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  // Navigation logic may go here --
  //   for example, create and push another view controller.
  AnotherViewController *anotherViewController = 
      [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
  [self.navigationController pushViewController:anotherViewController animated:YES];

Ok, so that makes another view slide on, and you can go back to the previous view ("pop" the current view) by clicking the button that automatically appears in the top left corner of the navigation bar now.

Ok, so SAY I want to populate the RIGHT SIDE of the navigation bar with a DONE button, like in the "Notes" app that comes with the iPhone. How would I do that?

I tried code like this:

  UIBarButtonItem * doneButton =
  [[UIBarButtonItem alloc]
   initWithBarButtonSystemItem:UIBarButtonSystemItemDone
   target:self
   action:@selector( doneFunc ) ];

  self.navigationController.navigationItem.rightBarButtonItem = doneButton ; // not it..

  [doneButton release] ;

doneFunc is defined, and everything, just the button never appears on the right side..

like image 935
bobobobo Avatar asked Nov 24 '09 04:11

bobobobo


1 Answers

The code you posted should work fine, I think. Question is, where did you put it? I would put it into the -viewDidLoad method of the view controller you're pushing in. If you need different buttons depending on the content you're showing then you could do it in the -viewWillAppear: method.

Update: Actually, I think you need to change

self.navigationController.navigationItem.rightBarButtonItem = doneButton;

to

self.navigationItem.rightBarButtonItem = doneButton;
like image 52
Thomas Müller Avatar answered Oct 23 '22 07:10

Thomas Müller