在以下情况下,单击铅笔将 iz 1 文本设置为可编辑,becomeFirstResponder
并打开键盘,我希望在单击“空行”或 iz2 时关闭键盘。
我怎样才能做到这一点?
我试过添加到 cellView
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self dismissKeyboard];
}
但它没有用
在以下情况下,单击铅笔将 iz 1 文本设置为可编辑,becomeFirstResponder
并打开键盘,我希望在单击“空行”或 iz2 时关闭键盘。
我怎样才能做到这一点?
我试过添加到 cellView
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self dismissKeyboard];
}
但它没有用
您可以使用以下方法隐藏键盘:
[self.view endEditing:YES];
编辑
在可以调用的地方添加手势becomeFirstResponder
。
- (void)showKeyboard
{
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
[self.tableView addGestureRecognizer:gesture];
}
并在 hide 方法中将其删除,
- (void)hide
{
[self.view endEditing:YES];
[self.view removeGestureRecognizer:self.view.gestureRecognizers[0]];
}
只需致电:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
或者:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSIndexPath *indexPathForYourCell = [NSIndexPath indexPathForRow:0 inSection:0];
YourCustomCell *cell = [self tableView:self.tableView cellForRowAtIndexPath:indexPathForYourCell];
[cell.iz1TextField resignFirstResponder];
}
我的最终解决方案类似于@caglar 在这里建议的是我在里面编写的代码tableviewcontroller
:
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
}
-(void)dismissKeyboard
{
[self.view endEditing:YES];
}