想写一个UINavigationBar的工具类,这个工具类需要传一个ViewController,来执行dismissViewControllerAnimated:YES completion:^{}方法,而我在具体的ViewController中初始化这个工具类的时候传的是self.
在加上UIBarButtonItem点击事件方法时会报错:*** Terminating app due to uncaught exception “”NSInvalidArgumentException””, reason: “”-[OfflineHistoryViewController backButtonHandler]: unrecognized selector sent to instance 0x7f81e3183440″”
请问问题出在哪里?
代码如下:
1. 工具类.h
在加上UIBarButtonItem点击事件方法时会报错:*** Terminating app due to uncaught exception “”NSInvalidArgumentException””, reason: “”-[OfflineHistoryViewController backButtonHandler]: unrecognized selector sent to instance 0x7f81e3183440″”
请问问题出在哪里?
代码如下:
1. 工具类.h
// // TitleBarHelper.h // #import <UIKit/UIKit.h> #import <Foundation/Foundation.h> @interface TitleBarHelper : NSObject { } @property(nonatomic, retain) UIViewController *viewController; @property(nonatomic, retain) NSString *titleStr; @property(nonatomic) BOOL needBack; - (id)initWithTitle:(NSString *)title ViewController:(UIViewController *)controller needBack:(BOOL)needBack; - (UINavigationBar *)getTitleBar; - (void)backButtonHandler; @end
2 工具类.m
// // TitleBarHelper.m #import "TitleBarHelper.h" #import "Public.h" @implementation TitleBarHelper { } // 初始化 -(id)initWithTitle:(NSString *)title ViewController:(UIViewController *)controller needBack:(BOOL) needBack{ self = [super init]; if (self) { self.viewController = controller; self.titleStr = title; self.needBack = needBack; } return self; } // 获取titleBar -(UINavigationBar *) getTitleBar{ UINavigationBar *titleBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, screen_width, Title_Height)]; [titleBar setBarTintColor:Main_Color]; [titleBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor whiteColor], NSForegroundColorAttributeName, nil]]; UINavigationItem *titleItem = [[UINavigationItem alloc] initWithTitle:self.titleStr]; if (self.needBack) { UIBarButtonItem *leftButton = [UIBarButtonItem new]; UIButton *btnView = [UIButton buttonWithType:UIButtonTypeCustom]; [btnView setFrame:CGRectMake(0, 0, 40, 40)]; [btnView setBackgroundImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateNormal]; [btnView addTarget:self.viewController action:@selector(backButtonHandler) forControlEvents:UIControlEventTouchUpInside]; [leftButton setCustomView:btnView]; [titleItem setLeftBarButtonItem:leftButton]; } [titleBar pushNavigationItem:titleItem animated:NO]; return titleBar; } // UIBarButtonItem点击事件 - (void)backButtonHandler { [self.viewController dismissViewControllerAnimated:YES completion:^{}]; } @end
3. 使用该工具类的ViewController.m
// // OfflineHistoryViewController.m #import "OfflineHistoryViewController.h" #import "TitleBarHelper.h" @interface OfflineHistoryViewController (){ UINavigationBar *titleBar; } @end @implementation OfflineHistoryViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor grayColor]; [self initView]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)initView{ [self initTitleBar]; } -(void)initTitleBar{ TitleBarHelper * titleBarHelper = [[TitleBarHelper alloc]initWithTitle:@"Title" ViewController:self needBack:YES]; titleBar = [titleBarHelper getTitleBar]; [self.view addSubview:titleBar]; } /* @end
解决方案:40分
[btnView addTarget:self.viewController action:@selector(backButtonHandler) forControlEvents:UIControlEventTouchUpInside];
self.viewController 改成self 试试