我在我的头文件中声明了这个:
UITapGestureRecognizer* tap;
并且在viewDidLoad
:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(hideKeyboard)];
tap.enabled = NO;
[self.view addGestureRecognizer:tap];
}
我添加了UITextFieldDelegate
,并添加了这个:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
tap.enabled = YES;
return YES;
}
如果我触摸键盘外的任何地方,它就会消失,但如果我触摸 aUIButton
它不会消失。
你知道为什么吗?