iOS Shared BarButtonItems
You can use the following UINavigationControllerDelegate method to set up the NavigationItems for all child ViewControllers:
Not-Long-Enough; Tell Me More Version
Here’s one difficulty I ran into this week that had a less-than-obvious solution…
In my iOS app, I wanted every UINavigationController’s child ViewController to have the same rightBarButtonItem.
Specifically, I wanted every child to show the “+” (Add) button and for the “+” button to behave the same, regardless of which screen it was touched in.
Unfortunately, storyboards don’t make this very easy to do in a DRY manner.
The solution basically has two parts:
- “Hack” the storyboard to add the shared BarButtonItem directly to the
- Subclass UINavigationController to programmatically add the shared BarButtonItem
to child ViewControllers.
- Subclass UINavigationController to programmatically add the shared BarButtonItem to child ViewControllers.
In order to display the common BarButtonItem, you will need the NavigationController to have a NavigationItem. However, by default a NavigationController object will contain a NavigationBar only and won’t allow adding any more objects.
To get around this, I discovered the following trick:
- Create a NavigationController object by dragging from the Object library onto an empty section of the canvas.
- Select the newly-created RootViewController.
Use the Editor menu to select Embed In->Navigation Controller.
The new NavigationController object will have a NavigationItem.
- Integrate the new NavigationController into your “actual” storyboard and clean up the unnecessary elements from the last few steps.
Now, you can drag a BarButtonItem onto the NavigationController’s NavigationItem.
This will help with visualizing how the navigationBar will look, but more importantly, it will also help with achieving DRYness, as shown in part two below.
Now, you will need to link the BarButtonItem to an IBOutlet and an IBAction:
- Create a subclass of UINavigationController.
- In the storyboard, set the Custom Class of the NavigationController to be your new class.
- Show the Assistant Editor and control-drag from the BarButtonItem into your new class’s .m file to create an outlet and an action for the BarButtonItem.
The final step is to programmatically add the BarButtonItem to all child ViewControllers. First, tell your new class to conform to the UINavigationControllerDelegate protocol. Then, add this code to your new class, replacing “addButton” with the name of your IBOutlet property:
IMPORTANT: Note the condition inside the “if” statement. This will allow you to override the rightBarButtonItem on a per-screen basis later, if you want. But for now, make sure you remove any rightBarButtonItems from child ViewControllers. Therefore, in your storyboard, your navigation path should look like this (only one “+” button):
That’s it! Now you can implement whatever you’d like in your IBAction method and the functionality will be the same on every screen.