在Objective C中跨类共享方法的最佳方法是什么?

Dyl*_*lan 14 objective-c ios

我有几个视图控制器和表视图控制器,我从我的根视图控制器推送.在所有这些中,我想在导航控制器中使用自定义后退按钮.我没有复制方法来将我的后退按钮设置到每个类文件中,而是创建了一个带有类方法的辅助类来完成设置.下面的代码有效,但我想知道我是否会以错误的方式解决这个问题.有没有更好的方法来实现这一目标?另外,我仍然在我的所有类中复制 - (void)myCustomBack方法,并且想知道是否有办法避免这种情况.

@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
Run Code Online (Sandbox Code Playgroud)

Dyl*_*lan 4

感谢大家的回复并为我指明了正确的方向。我最终创建了一个 UIViewController 类别,其中包含我想要在其他类中使用的方法。如果有更优雅的方法来做到这一点,我很高兴听到建议。这就是我所选择的:

// UIViewController+BackButtonSetup.h

@interface UIViewController (BackButtonSetup)
- (void)backButtonSetup;
- (void)myCustomBack;
@end


// UIViewController+BackButtonSetup.m

@implementation UIViewController (BackButtonSetup)

- (void)backButtonSetup
{
    self.navigationItem.hidesBackButton = YES;

    // A Bunch of code to set up the custom back button

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

}

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

@end


// MyCustomTableViewController.m

#import "UIViewController+BackButtonSetup.h"

@implementation MyCustomTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self backButtonSetup];
}
@end


//MyCustomViewController.m

#import "UIViewController+BackButtonSetup.h"

@implementation MyCustomViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self backButtonSetup];
}
@end
Run Code Online (Sandbox Code Playgroud)