【问题标题】:Dismiss Keyboard when touch began on UITableView and GoogleMap's View在 UITableView 和 GoogleMap 的视图上开始触摸时关闭键盘
【发布时间】:2013-12-03 09:27:17
【问题描述】:

我的项目设计:

UINavigationController ---> UITableViewController ---> UIViewController

我在navigationItem.titleView 上有一个UISeachBar

我尝试使用UITapGestureRecognizer,它可以工作,但不如预期。仅当我触摸 UITableViewCell 时键盘才会关闭。

我希望UITableView 响应touchesBegan,这样我就可以让键盘在我触摸UITableViewCell 或只是在桌面上滚动时消失。

除此之外,我的 UIViewController 包含一个 googlemap 的视图,它确实响应 touchesBegan 但只有第一次触摸,之后,所有其他触摸都将被忽略。

有人可以帮忙吗?

太长了,没看:我想关闭 UITableView 上的虚拟键盘喜欢 safari 或 chrome:触摸开始时,而不是结束时

【问题讨论】:

  • 你试过继承 UITableView 并覆盖触摸事件吗?

标签: ios objective-c uitableview keyboard


【解决方案1】:

试试这个

[self.view endEditing:YES];

【讨论】:

    【解决方案2】:

    尝试像这样创建 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];

    【讨论】:

    • 我按照你说的试过了。当我触摸键盘外部时,它确实关闭了键盘。然而,UITableViewCells 上的触摸事件被取消,即使是 [super touchesBegan:withEvent:]。你能给我一些进一步的帮助吗?谢谢。
    • 你在 UITableViewCell 中做了哪些事件?你能提供一些代码吗?我已经通过子类化 UITableView 对其进行了测试,并且 UITableViewCell 的事件没有被覆盖,也许你正在做其他事情
    • 感谢 Joshua,这是我的错。我在UITableView 上使用了Category 并实现了touchesBegan:withEvent:。这就是UITableViewCells 活动被取消的原因。但是,我不明白子类化和类别这两者之间的区别,以及为什么类别会取消UITableViewCell 上的事件而子类化不会。
    • 嗨@PhamHoan,类别与子类已在此处回答stackoverflow.com/a/522942/2301271
    • 好的,抱歉,我忘了标记你的答案。对于那些和我有同样问题的人。按照乔希的回答并实施[scrollViewWillBeginDragging:]。您将能够通过触摸或拖动 UITableView 来关闭键盘。
    【解决方案3】:

    试试这个...

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

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

    【讨论】:

      猜你喜欢
      • 2012-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-22
      • 2011-07-15
      相关资源
      最近更新 更多