【问题标题】:Hide Keyboard when user taps outside the text field in UITableView and UIScrollView当用户在 UITableView 和 UIScrollView 中的文本字段之外点击时隐藏键盘
【发布时间】:2012-05-13 00:30:10
【问题描述】:

我的 UIViewController 层次结构如下

UIView
    UIScrollView
        UITableView
            UITableViewCell
                UITextField

UITableView 以编程方式添加到视图控制器。 当用户在视图或 UITableView 上点击 UTTextField 外部时,我想隐藏键盘 当用户点击其他 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

还有其他隐藏键盘的方法吗?

【问题讨论】:

  • 在任何情况下,您都需要检测事件并使用 `[txtfield resignFirstResponder];` 而不获取触摸事件或检测任何您无法做到的事件..
  • 如果用户触摸视图,你想退出键盘
  • 只需将手势识别器添加到表格背景:[tableview.backgroundView addGestureRecognizer:singleTap];这意味着 didSelectRowAtIndexPath 仍然可以工作。

标签: iphone ios5


【解决方案1】:

当 UITableview 为编辑模式时,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"); 
}

【讨论】:

    【解决方案2】:

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

    添加

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        使用 UITextFieldDelegate 和方法

        – textFieldShouldEndEditing:(UITextField*) txtField
        {
        
        [txtField resignKeyPads];
        return YES:
        }
        

        这也可以通过scrollview delgate来完成

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

        还有一件事是将 UIView 的类更改为 UIControl 并创建一个方法 IBAction 并将 UIControl touchupInside 连接到该 ibaction,它将退出键盘

        【讨论】:

        • 你的意思是在scrollview滚动时防止隐藏?
        • – textFieldShouldEndEditing:(UITextField*)txtField 仅在用户点击返回键时隐藏键盘。使用滚动视图委托-(void) scrollViewWillBeginDragging:(UIScrollView *)scrollView 滚动出现在某些条件下,而不是在运行时的某些条件下
        • 另一件事是让 UIControlClass 看到已编辑的答案
        • 如果不明白再ping我
        • 我认为将 UIView 更改为 UIControl 并添加 touchupInside 事件不是正确的选择。这是我最后的选择。谢啦。感谢您的建议
        【解决方案5】:

        如果您仍想使用点击手势,则需要将手势识别器添加到表格背景中,如下所示:

        [tableView.backgroundView addGestureRecognizer:singleTap];
        

        这将防止隐藏:

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

        【讨论】:

        • 尝试将点击手势添加到 backgroundView 无法捕获点击事件。这是我的代码[[optionsTableView backgroundView] addGestureRecognizer:singleTap];
        猜你喜欢
        • 1970-01-01
        • 2011-03-30
        • 2012-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-22
        • 1970-01-01
        相关资源
        最近更新 更多