Adding a refresh UIBarButtonItem to a UINavigationController loaded from a NIB
Asked Answered
M

3

10

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 navigation 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 navigationController'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!

Megaera answered 19/10, 2010 at 19:36 Comment(0)
H
20

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.

Herbherbaceous answered 19/10, 2010 at 19:49 Comment(1)
Nimrod, you rock! An explanation even I can follow!Megaera
F
1

Worked on SWIFT 4.1

navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: 
UIBarButtonSystemItem.refresh, target: self, action: 
#selector(RootViewController.refresh))
Freedman answered 14/12, 2018 at 5:17 Comment(0)
C
0

Copy-paste solution:

override func viewDidLoad() {
    super.viewDidLoad()
    navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .refresh, target: self, action: #selector(refresh))
}

@objc func refresh() {
    print("Refreshing.")
}
Colewort answered 28/10, 2020 at 10:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.