Question

I've noticed I have a set of UIViewController's for a product filtering system that use the exact same code. This code basically creates buttons for a UIToolBar and hides the UIToolBar of the controller that presented it. The only difference will be the title that says "refine by:.

On each page I'll have the type of page it is after "refine by" so for example: On the gender filtering page it will say "Refine by: then I'll append another string in bold that says "Gender" for the products type page I'll append a bold string that says "Product type" and so on.

Here is the code that is repeated in each of the custom classes connected to these controllers:

@property (weak, nonatomic) IBOutlet UIToolbar *toolBar;


@end

@implementation VAGRefineProductTypeViewController
{
    VAGRefineProductTypeViewController *_thisController;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        // Custom initialization
        _thisController = self;
    }
    return self;

}

- (void)viewDidLoad
{

UIToolbar *toolBar = [_thisController toolBar];

[[_thisController navigationItem] setLeftBarButtonItem:nil];
[_thisController setTitle:@"R  E  F  I  N  E     B  Y:"];

// Setup buttons for tool bar
UIButton *clearButton = [[UIButton alloc] initWithFrame:CGRectMake(40, 3, 110, 38)];
UIButton *doneButton = [[UIButton alloc] initWithFrame:CGRectMake(170, 3, 110, 38)];

[clearButton setBackgroundColor:[UIColor darkGrayColor]];
[doneButton setBackgroundColor:[UIColor blackColor]];

[clearButton setTitle:@"Clear" forState:UIControlStateNormal];
[doneButton setTitle:@"Done" forState:UIControlStateNormal];

[clearButton addTarget:_thisController action:@selector(clearButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[doneButton addTarget:_thisController action:@selector(doneButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

// Add buttons to toolbar
[toolBar addSubview:clearButton];
[toolBar addSubview:doneButton];

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    // Hide previous pages tool bar
    [[_thisController navigationController] setToolbarHidden:YES animated:NO];
}

- (IBAction)clearButtonTapped:(id)sender {
    NSLog(@"CLEAR BUTTON TAPPED");
}

- (IBAction)doneButtonTapped:(id)sender {
    NSLog(@"DONE BUTTON TAPPED");
}

This UIToolBar is added to each UIViewController in interface builder. The one that is hidden in viewWillAppear is the one added programmatically from UIViewController that presented the current UIViewController.

Question:

Rather than copying and pasting this code into each UIViewController's class file how can I put it in a file and use that file within each UIViewController?

Was it helpful?

Solution

You have various options. Probably the simplest is to define a base UIViewController class that defines the shared properties and methods, and then make all the view controllers that use these properties subclasses of that class.

Another option would be to define a category of UIViewController that defines new methods. That doesn't let you add new properties or instance variables to your view controllers however (at least not without getting tricky.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top