Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to share methods across classes in Objective C?

I have several view controllers and table view controllers that I push from my root view controller. In all of these I'd like to use a custom back button in the navigation controller. Instead of copying the method to set up my back button into each class, file, I've created a helper class with a class method that does the setup. The code below works, but I'm wondering if I'm going about this the wrong way. Is there a better way to achieve this? Also, I'm still duplicating the -(void)myCustomBack method in all my classes and was wondering if there's a way to avoid that as well.

@interface NavBarBackButtonSetterUpper : NSObject
+ (UIButton *)navbarSetup:(UIViewController *)callingViewController;
@end

@implementation NavBarBackButtonSetterUpper

+ (UIButton *)navbarSetup:(UIViewController *)callingViewController
{
    callingViewController.navigationItem.hidesBackButton = YES;

    UIImage *backButtonImage = [[UIImage imageNamed:@"back_button_textured_30"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 5)];

    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];

    [backButton setBackgroundImage:backButtonImage forState:UIControlStateNormal];

    [backButton setTitle:@"Back" forState:UIControlStateNormal];
    backButton.titleLabel.font = [UIFont fontWithName:@"AmericanTypewriter-Bold" size:12];
    backButton.titleLabel.shadowOffset = CGSizeMake(0,-1);

    UIView *customBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
    [customBackView addSubview:backButton];

    callingViewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:customBackView];

    return backButton;
}
@end



@interface MyCustomTableViewController : UITableViewController
@end

@implementation MyCustomTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIButton *backButton = [NavBarBackButtonSetterUpper navbarSetup:self];

    [backButton addTarget:self  action:@selector(myCustomBack) forControlEvents:UIControlEventTouchUpInside];
}

- (void)myCustomBack
{
    [self.navigationController popViewControllerAnimated:YES];
}
@end


@interface MyCustomViewController : UIViewController
@end

@implementation MyCustomViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIButton *backButton = [NavBarBackButtonSetterUpper navbarSetup:self];

    [backButton addTarget:self  action:@selector(myCustomBack) forControlEvents:UIControlEventTouchUpInside];
}

- (void)myCustomBack
{
    [self.navigationController popViewControllerAnimated:YES];
}
@end
like image 428
Dylan Avatar asked Mar 30 '13 18:03

Dylan


1 Answers

I think that the solution to your problem would be inheritance.

You could create a subclass of UIViewController and have all of your custom view controllers inherit from this custom subclass. The "parent" subclass would have the -(void)myCustomBack as well as the setup code and you wouldn't need to repeat it anymore.

like image 174
Pinwheeler Avatar answered Sep 30 '22 15:09

Pinwheeler