我有几个视图控制器和表视图控制器,从我的根视图控制器.在所有这些中,我想在导航控制器中使用自定义后退按钮.而不是复制方法来设置我的后退按钮到每个类,文件,我已经创建了一个帮助类与类方法进行安装.下面的代码可以正常工作,但是我想知道我是否会错过这个方法.有没有更好的方式来实现这一点?此外,我仍然在我的所有类中复制 – (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,5)]; UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(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,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