2012-05-03 23 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