使用UITapGestureRecognizer时,不会调用textFieldShouldEndEditing

时间:2013-04-08 09:07:53

标签: iphone ios objective-c

在我的ViewController中,我得到了-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath,但这永远不会被调用。我搜索了为什么没有被调用,我发现我使用它:

 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(dismissKeyboard)];

    [self.view addGestureRecognizer:tap];

在点按背景时关闭键盘。任何想法我如何解决这个问题,解雇键盘背景和didSelectRowAtIndexPath工作?

5 个答案:

答案 0 :(得分:0)

didSelectRowAtIndexPath永远不会被调用,因为您可能没有实现UITableViewDelegate

你应该只使用你的控件来从响应者那里辞职

 [yourTextField resignFirstResponder];

答案 1 :(得分:0)

尝试这样它会帮助你,

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    if(![[touch view] isKindOfClass:[UITextField class]]){

//resign your textfield here
    }

}

答案 2 :(得分:0)

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                               initWithTarget:self
                               action:@selector(dismissKeyboard:)];

[tbl addGestureRecognizer:tap];

-(void)dismissKeyboard:(UITapGestureRecognizer *)gestureRecognizer
{
    CGPoint p = [gestureRecognizer locationInView:tbl];

    NSIndexPath *indexPath = [tbl indexPathForRowAtPoint:p];
    if (indexPath == nil)
        NSLog(@"Resign");
    else
        NSLog(@"Did select Row, %i",indexPath.row);// Do your stuff as you are doing in didSelectRow
}

实施此

答案 3 :(得分:0)

如果点按此视图。您必须覆盖委托方法shouldReceiveTouch才能返回NO。如果在tableView

上发生了触摸
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

    // Disallow recognition of tap gestures in the segmented control.
    if ([touch.view isDescendantOfView:self.tblView]) {
        return NO;
    }
    return YES;
}

现在两个委托方法将触发.. Note: self.tblView是一个tableView

答案 4 :(得分:0)

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 
return TRUE;

好的,看看你是如何继续使用那个......然后如果它有效,请阅读文档以获得详尽的解释,以便你清楚地理解。