views:

25

answers:

1

Hi all, I'm a complete iphone/obj-c noob, and this site has proven invaluable for finding my way around the platform.

I have a UINavigationController loading in the first tab of a UITabBarController. I'd like add a refresh button on the right side of the UINavigationItem. My dim understanding is that one has to set properties like titles and buttons on a nav controller before it's added to the control hierarchy. I have no problem adding buttons to the child nav controller, but I'm stumped by how to change the root, since it's loading from the nib.

So in the IB screencap below (oops, not enough reputation points, let me try to diagram this....), I'm trying to add a refresh button to the selected item.

  • File's Owner
  • First Responder
  • App Delegate
  • Window
  • Tab Bar Controller
    • Tab Bar
    • Observation List View Controller
      • Navigation Bar
      • View Controller
        • Table View
        • Navigation Item
      • Tab Bar Item
    • Another List View Controller
    • etc.
    • etc.

I've tried to add the button in the nav controller's viewDidLoad, with no help:

- (void)viewDidLoad {
[super viewDidLoad]; 

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
         initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
         target:self
             action:@selector(refresh:)];
}

I've also tried to programmatically add my ObservationListViewController to the UITabBarController, and insert the button in that block, but I'm way out of my depth.

Any idea how I could inject adding a button into the process of loading a nav controller from the nib?

Thank you!

A: 

It looks like your viewDidLoad method is allocating but not releasing the bar button item. you should do this:

UIBarButtonItem *button = [[UIBarButtonItem alloc]
     initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
     target:self
         action:@selector(refresh:)];
self.navigationItem.rightBarButtonItem = button;
[button release];

You need to do this not in the nav controller, but in the root view controller that's contained within the navigation controller. In other words, put it in "View Controller" not "Observation List View Controller"

Each controller you push onto the nav controller can have it's own self.navigationItem.whatever and this will be displayed by the nav controller when the new controller is pushed onto its stack.

Nimrod
Nimrod, you rock! An explanation even I can follow!
Scott