2012-05-03 38 views
1

我的UIViewController层次如下隐藏键盘时的UITableView和的UIScrollView文本字段以外用户抽头

UIView 
    UIScrollView 
     UITableView 
      UITableViewCell 
       UITextField 

的UITableView的被添加到视图控制器编程。 我想,当用户点击UTTextField外无论是在视图或在UITableView的 我执行一些方法来隐藏键盘,当用户点击其他的UITableView行

我试图

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 

的UIScrollView不发触摸事件。

我尝试添加双击手势

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)]; 
[singleTap setNumberOfTapsRequired:1]; 

[[self view] addGestureRecognizer:singleTap]; 

但TapGesture,隐藏了以下事件

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

,还有没有其他可能的方式来隐藏键盘?

+0

在您需要检测事件并使用' 无论如何[txtfield resignFirstResponder];'没有得到触摸事件或发现任何情况下你是不是能够做到这一点.. – Nit

+0

做ü要辞职的键盘,如果用户触摸浏览 – Saad

+0

只需将手势识别器添加到表格背景:[tableview.backgroundView addGestureRecognizer:singleTap];这意味着didSelectRowAtIndexPath将仍然有效。 – shoughton123

回答

1

使用UITextFieldDelegate 和方法

– textFieldShouldEndEditing:(UITextField*) txtField 
{ 

[txtField resignKeyPads]; 
return YES: 
} 

这也可以通过scrolview delgate做得太

-(void) scrollViewWillBeginDragging:(UIScrollView *)scrollView 
{ 
    //resign all keypads of all textfields use array containing keypads 
} 

一件事是类的UIView改变UIControl并作出方法IBAction为并将UIControl touchupInside连接到该ibaction,它将会退出键盘

+0

你的意思是如果滚动浏览滚动,防止隐藏? – Saad

+0

' - textFieldShouldEndEditing:(UITextField *)txtField'只有当用户点击返回键时才隐藏键盘。使用scrollview委托' - (无效)scrollViewWillBeginDragging:(UIScrollView *)scrollView'滚动显示的一些条件,并没有在运行时的某些条件 – Naresh

+0

是的,但是当你滚动tableview它将工作 – Saad

0

如果您想要继续使用轻击手势,则需要添加手势识别器表中的背景像这样:

[tableView.backgroundView addGestureRecognizer:singleTap]; 

这将防止的遮盖:

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
+0

尝试向背景视图添加轻击手势无法捕获轻击事件。这里是我的代码'[[optionsTableView backgroundView] addGestureRecognizer:singleTap];' – Naresh

2

使用的代码:[self.view endEditing:YES];

+0

把这个放在哪里? TouchBegan方法里面的 –

+0

.. – NiKKi

0

如果你想在背景视图中放置一个手势识别器,你需要确保它有一个手势识别器。

添加时的UITableView为编辑模式

self.tableView.backgroundView = [[UIView alloc] initWithFrame:self.tableView.bounds]; 
0

的UITableView didSelectRowAtIndexPath方法不会叫。所以你想创建自定义手势事件来处理相同的事情。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
//cell design code goes here. 
UITapGestureRecognizer *doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)]; 
doubleTapGestureRecognizer.numberOfTapsRequired = 1; 
//tapGestureRecognizer.delegate = self; 
[cell addGestureRecognizer:doubleTapGestureRecognizer]; 
return cell; 
} 
//Handle the click event 
-(void) handleDoubleTap:(UITapGestureRecognizer*)sender{ 
[self.view endEditing:YES]; 
UITableViewCell * cell =(UITableViewCell*) sender.view; 
//get the selected table indexpath. 
NSIndexPath * indexPath= [tblCart indexPathForCell:cell]; //to handle the scroll 
tblCart scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; 
NSLog(@"Comming"); 
} 

我希望它能帮助你解决同样的问题。 http://greatindiaclub.oliwy.net/?p=1180

相关问题