I have a problem in my iOS application that I'm looking for some help with. I'm relatively new to iOS programming to I'm sure that there is some relatively simple solution to my problem.
First, I'm going to explain the hierarchy of the application:
- It uses a
UITabBarController
to show a couple of different screens. - It uses a
SWRevealViewController
to show a sidebar - The sidebar is accessed from a Bar button item that is present in the Navigation bar of the application.
That the application uses SWRevealViewController
https://github.com/John-Lluch/SWRevealViewController doesn't directly affect the problem that I have. If you are not familiar with this code base, just think of a Simple Bar Button that is shown at all times.
Now to the problem:
- The Bar button that I want to show is associated with a few lines of code. (A
property
declaration and some methods). - This code should be used on a major part of the different view controllers in the application.
Now in the normal case, I'd just subclass UIViewController
and make it the superclass of all my views that should show this button. However, my application should also show other types of views, such as a UITableViewController
, so subclassing doesn't solve the entire problem.
If Objective-C
supported Multiple Inheritance, I would make a class containing this code and let my other classes extend any subclass of UIViewController
and my ugly support class at the same time.
Notes:
- For now, my app is based on Storyboards
- The
TabBarController
points to a number ofUINavigationController
s, and not to any views that doesn't have a Navigation Bar. - I have tried implementing this with
Objective-c
Category
where I add a category to UIViewController that does setup of my UIViewController. But I got stuck on this solution when I needed to add a@Property
for the button and linking it to the XIB/Storyboard. Got the idea from this post Add the same UIBarButtonItem to several UIViewControllers but it doesn't contain any details.
tl;dr: I want to show the very same UIBarButtonItem
on many of my applications views. The UIBarButtonItem
is associated with some code, that is also the same for all these views.
What would be a good way to achieve this?