所以我有一个视图控制器和一个辅助类
//ViewController.h
TextFieldHelper *textFieldHelper = [[TextFieldHelper alloc] initWith:self.containerView scrollView:self.scrollView viewController:self];
[textFieldHelper runTextFieldHelper];
这是我的TextFieldHelper类
//TextFieldHelper.h
@interface TextFieldHelper : NSObject <UITextFieldDelegate>
@property (nonatomic) UIViewController *viewController;
@property (nonatomic) UIView *view;
@property (nonatomic) UIScrollView *scrollView;
@property (nonatomic) UITextField *activeField;
- (instancetype)initWith:(UIView *)view scrollView:(UIScrollView*)scrollView viewController:(UIViewController *)viewController;
- (void)runTextFieldHelper;
@end
//TextFieldHelper.m
- (instancetype)initWith:(UIView *)view scrollView:(UIScrollView *)scrollView viewController:(UIViewController *)viewController {
self = [super init];
if (self) {
// self.delegate = self;
self.viewController = viewController;
self.view = view;
self.scrollView = scrollView;
}
return self;
}
#pragma mark - run the text field helper
- (void)runTextFieldHelper {
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(hideKeyboard)];
[self.view addGestureRecognizer:tap];
[self registerForKeyboardNotifications];
}
- (void)hideKeyboard {
NSLog(@"%@",@"hello world");
[self.view endEditing:YES];
}
每次我点击视图时,hideKeyboard方法都不会运行。我在这里缺少什么?
谢谢!
答案 0 :(得分:1)
只需替换
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self.viewController
action:@selector(hideKeyboard)];
与
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(hideKeyboard)];