0

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 of UINavigationControllers, 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?

Community
  • 1
  • 1
Joakim
  • 3,224
  • 3
  • 29
  • 53

2 Answers2

0

If i understood you correctly.
You want to show button for all your UIViewControllers.

There are many ways to achieve this. Please try this one.
1. Create a subclass of UIView with its XIB. It contains your button. create Properties and IBAction.
2. Implement your action on this subclass.
3. On .m file write the below code

 - (id)awakeAfterUsingCoder:(NSCoder *)aDecoder
{
    if (![self.subviews count])
    {
        NSBundle *mainBundle = [NSBundle mainBundle];
        NSArray *loadedViews = [mainBundle loadNibNamed:@"MyView" owner:nil options:nil];
        return [loadedViews firstObject];
    }
    return self;
}


where, MyView is the name of your view.
4. Drag and drop a UIViewand place it on every `UIViewController XIB (or storyboard in your case) and set its custom class to "MyView" (or the name of your newly created class).
5. Run your project.

For reference: May i help you?

Abdul Yasin
  • 3,480
  • 1
  • 28
  • 42
  • I'm not sure if this would solve my problem... Does the "set it's custom class to MyView" refer to only the common view, or to all views in my project? – Joakim May 12 '15 at 08:55
  • Add a placeholder view for every view controllers and set custom class to MyView for every view controllers. – Abdul Yasin May 12 '15 at 09:11
0

I have now solved this problem and I will post it here for reference:

This solves the problem with adding a sidebar button to several views in the project, but the code should be usable for any UIBarButton. The solution is the create a Category for UIViewController that specifies a setup method. This setup method is called from the different Views.

Category .h file:

#import <UIKit/UIKit.h>
@interface UIViewController (SidebarCompliance)
- (void)setupSidebar;
@end

Category .m file:

#import "UIViewController+SidebarCompliance.h"
#import "SWRevealViewController.h"
@implementation UIViewController (SidebarCompliance)
- (void)setupSidebar {
    SWRevealViewController *revealViewController = self.revealViewController;
    if (revealViewController) {
        UIBarButtonItem *sidebarButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu.png"] landscapeImagePhone:[UIImage imageNamed:@"menu.png"] style:UIBarButtonItemStylePlain target:self.revealViewController action:@selector(revealToggle:)];
        self.navigationItem.rightBarButtonItem = sidebarButton;
        [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
    }
}
@end

And then an example of one of my views:

View .h file:

#import <UIKit/UIKit.h>
#import "UIViewController+SidebarCompliance.h"
@interface NumeroUno : UIViewController
@end

View .m file:

#import "NumeroUno.h"
@implementation NumeroUno
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupSidebar];
}
@end
Joakim
  • 3,224
  • 3
  • 29
  • 53