热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

键盘解除和UITableView触摸问题-KeyboarddismissandUITableViewtouchissue

SoIhavethisUITableViewthatcontainsfewrows,whenclickedonarow,anotherUIViewControlleri

So I have this UITableView that contains few rows, when clicked on a row, another UIViewController is pushed with the specific row details. My problem is that I want to dismiss keyboard with any touch on the screen (also on the UITableView), but when I do it, and click on the UITableView row, the keyboard dismisses, but the didSelectRow method is not working. It wont push the new ViewController once clicked on the cell.

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

Also the other way around, once I change it, you can click on the TableView cell, the keyboard dismiss and the other ViewController is pushed, but the touch on any other place is not dismissing the keyboard.

另一方面,一旦我改变它,你可以点击TableView单元格,键盘消除,另一个ViewController被推,但任何其他地方的触摸不解除键盘。

What should I do?

我该怎么办?

- (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];
}

Thanks!

2 个解决方案

#1


2  

set

[gestureRecognizer setCancelsTouchesInView:NO];

and instead of adding it to the tvFoods, add to entire self.view, so it won't affect overridden definitions.

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

also, remove the shouldReceiveTouch delegate, it is not necessary

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

#2


1  

Using whiteagle's tip, I use the following code to trap a single tap to close the keyboard in a UITableView. It passes the tap through and still allows a row to be selected. In my case, the tap being passed through triggers an inline date picker to be displayed.

使用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];
  }

推荐阅读
author-avatar
承志68694849
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有