在下面的场景中,单击铅笔会使iz 1文本变为可编辑,方法是将其设为becomeFirstResponder
并打开键盘,我希望在单击“空行”或iz2时关闭键盘。
我该怎么做?
我已尝试添加到cellView
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self dismissKeyboard];
}
但它无效
答案 0 :(得分:1)
您可以使用以下方法隐藏键盘:
[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]];
}
答案 1 :(得分: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];
}
答案 2 :(得分:0)
我的最终解决方案类似于@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];
}