0

所以我有这个UITableView包含几行,当点击一行时,另一个UIViewController被推送特定行的详细信息。我的问题是我想通过屏幕上的任何触摸(也在UITableView)上关闭键盘,但是当我这样做并单击UITableView行时,键盘会关闭,但该didSelectRow方法不起作用。ViewController一旦单击单元格,它就不会推送新的。

同样反过来,一旦我更改它,您可以单击 TableView 单元格,键盘关闭并且另一个 ViewController 被推送,但是任何其他地方的触摸都不会关闭键盘。

我该怎么办?

- (IBAction)hideKeyboard:(id)sender
{
    [tfFoodsSearchQuery resignFirstResponder];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

    if ([touch.view isKindOfClass:[UITableView class]])
    {
        // we touched a button, slider, or other UIControl
        return YES; // handle the touch
    }
    NSLog(@"Touch");
    return NO; // ignore the touch
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

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

    [self.view addGestureRecognizer:tap];

    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard:)];
    gestureRecognizer.delegate = self;
    [self.tvFoods addGestureRecognizer:gestureRecognizer];
}

谢谢!

4

2 回答 2

2

[gestureRecognizer setCancelsTouchesInView:NO];

而不是将其添加到 tvFoods,而是添加到整个 self.view,因此它不会影响覆盖的定义。

另外,删除 shouldReceiveTouch 委托,没有必要

于 2012-10-28T14:30:11.467 回答
1

使用 whiteagle 的提示,我使用以下代码捕获单击以关闭 UITableView 中的键盘。它通过水龙头,仍然允许选择一行。在我的情况下,通过的点击会触发显示内联日期选择器。

  -(void)viewDidLoad {
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    [singleTap setNumberOfTapsRequired:1];
    [singleTap setNumberOfTouchesRequired:1];
    [singleTap setCancelsTouchesInView:NO];
    [self.view addGestureRecognizer:singleTap];
    singleTap = nil;
  }

  // 
   -(IBAction)hideKeyboard {
    [activeTextField resignFirstResponder];
  }
于 2013-12-21T02:47:39.390 回答