11
votes

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

2

2 Answers

32
votes

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;
13
votes

AH. You have to do:

- (void)tableView:(UITableView *)tableView
        didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  AnotherViewController *anotherViewController = 
      [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
  [self.navigationController pushViewController:anotherViewController animated:YES];


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

  anotherViewController.navigationItem.rightBarButtonItem = doneButton ;

  [doneButton release] ;
}

Who'da thunk it.