我在我的iPhone应用程序中遇到了一个问题,由于我有一个UITableView
占据了屏幕的其余空间,所以即使将UIView
设置为控制器,它也是不可触摸的。
我想知道如何通过点击UITableView
来重新设置键盘,也就是firstResponder
?是否有一种方法可以监视UITableView
上的触摸事件,即使不是选择可点击单元格的方式。
基本上,如果单元格触发事件,我知道如何取消键盘,但是,如果我单击UITableView
的不可点击部分,我仍希望键盘消失。
我在我的iPhone应用程序中遇到了一个问题,由于我有一个UITableView
占据了屏幕的其余空间,所以即使将UIView
设置为控制器,它也是不可触摸的。
我想知道如何通过点击UITableView
来重新设置键盘,也就是firstResponder
?是否有一种方法可以监视UITableView
上的触摸事件,即使不是选择可点击单元格的方式。
基本上,如果单元格触发事件,我知道如何取消键盘,但是,如果我单击UITableView
的不可点击部分,我仍希望键盘消失。
两种选择:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[self.view endEditing:YES];
}
个人而言,我通常在表格滚动时这样做,因为我不喜欢单击即可关闭键盘。
添加轻拍手势识别器是一个有趣的解决方案,但是还有一种选择,您不需要编写任何代码!
您可以在Interface Builder中设置属性keyboardDismissMode为“Dismiss on drag”,使其适用于您的表视图。它是从UIScrollView继承的属性,每当您滚动表视图时,键盘就会被取消。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITapGestureRecognizer *doubleTap =
[[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(tapDetected:)];
doubleTap.numberOfTapsRequired = 1;
[self.tableView addGestureRecognizer:doubleTap];
[doubleTap release];
}
- (IBAction)tapDetected:(UIGestureRecognizer *)sender
{
CGPoint p = [sender locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p];
if(indexPath == nil)
{
NSLog(@"empty");
}
else
{
[textField resignFirstResponder];
}
}
我认为这将有助于...试一试吧...
@property (weak, nonatomic) UITextField *activeTextField; // keeps track of whose keyboard is being displayed.
- (void)textFieldDidBeginEditing:(UITextField *)textField {
self.activeTextField = textField;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// if (indexPath equal to something then)
[self.activeTextField resignFirstResponder];
}