在UITableView和GoogleMap的View上开始触摸时关闭键盘



我的项目设计:

UINavigationController ---> UITableViewController ---> UIViewController

我在navigationItem.titleView 上有一个UISeachBar

我试着使用UITapGestureRecognizer,它可以工作,但并不像预期的那样。只有当我触摸UITableViewCell时,键盘才会关闭。

我想要UITableView响应touchesBegan,这样每当我触摸UITableViewCell或只是在桌子上滚动时,我就可以让键盘消失。

除此之外,我的UIViewController包含谷歌地图的View,它确实对touchesBegan有响应,但只有一次第一次触摸,之后,每隔一次触摸都会被忽略。

有人能帮忙吗?

太长,没有读取:我想关闭UITableView上的虚拟键盘,比如safari或chrome:当触摸开始时,而不是当它结束

试试这个

[self.view endEditing:YES];

尝试创建UITableView的子类,如下所示:

subclassTB.h
 #import <UIKit/UIKit.h>
@interface subClassTB : UITableView
@end

subclassTB.m
#import "subclassTB.h"

 @implementation subClassTB
 - (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    // do your stuff here 
    //NSLog(@"tap registered");
    [super touchesBegan:touches withEvent:event];
}
@end

并且在您当前的控制器中使用初始化表格

subclassTB *yourtable = [[subclassTB alloc]initWithFrame:youFrame style:yourStyle];

试试这个。。。

添加一个覆盖整个视图(设备大小)的空图像视图,然后为该图像视图添加手势识别器,然后在回调方法中编写以下代码。

-(void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UIImageView *image;//your IBOutlet imageview
    UITapGestureRecognizer *recog=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(called:)];
    [image addGestureRecognizer:recog];
}    
-(void)called:(UITapGestureRecognizer *)sender
{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}

最新更新