views:

1254

answers:

3

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

+1  A: 

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;
Thomas Müller
No, tried that.. but it was helpful. I moved it to viewDidAppear. It seems the problem is the right button gets _HIDDEN_ whenever a new view controller is pushed on top of the stack
bobobobo
I've updated my answer. I think there is a bug in your code. Let me know if this fixes it.
Thomas Müller
No, I did try that, but the rightBarButtonItem still __disappears__ once the new view controller is pushed on.
bobobobo
You have to do all that in the *new* view controller, the one that you're pushing in.
Thomas Müller
+1  A: 

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.

bobobobo
A: 

The above code by user "bobobobo" works good. But the only problem I notice is: When the new view comes up, it somehow brings up the Done button once to the left for few millisecs before it actually shows the Done button on the right top nav bar area. I don't understand why. Any clue?

Thanks.

sg