1

My project design:

UINavigationController ---> UITableViewController ---> UIViewController

I've a UISeachBar on navigationItem.titleView

I tried to use UITapGestureRecognizer, it works but not as expected. Keyboard only dismisses when I touch on an UITableViewCell.

I want UITableView responds to touchesBegan so that I could make the keyboard disappears whenever I touch on an UITableViewCell or just scroll on the table.

Besides that, my UIViewController contains a googlemap's View, it does respond to touchesBegan but only one first touch, after that, every other touches would be ignored.

Can anyone help, pls ?

too long, didn't read: i want to dismiss the virtual keyboard on UITableView likes safari or chrome: when the touch began, not when it ended

Pham Hoan
  • 2,107
  • 2
  • 20
  • 34

3 Answers3

2

Try this

[self.view endEditing:YES];
Himanshu Gupta
  • 207
  • 1
  • 6
1

Try this...

Add an empty imageview covering whole view(size of device), then add the gesture recognizer for that imageview and then in the call back method write the below code .

-(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];
}
Muthu Sabarinathan
  • 1,198
  • 2
  • 21
  • 49
1

Try creating a subclass of UITableView like so:

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

and in your current controller initialize the table by using

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

Joshua
  • 2,432
  • 1
  • 20
  • 29
  • I've tried as you said. It did dismiss the keyboard when I touched outside the keyboard. However, the touch event on `UITableViewCell`s got cancelled, even with `[super touchesBegan:withEvent:]`. Can you give me some further help ? thank you. – Pham Hoan Dec 06 '13 at 10:36
  • What events were you doing in UITableViewCell? and can you provide some codes. I already tested it by subclassing UITableView and the UITableViewCell's event are not being overridden maybe you are doing something else – Joshua Dec 09 '13 at 01:59
  • thank Joshua, it was my fault. I used `Category` on `UITableView` and implemented `touchesBegan:withEvent:`. That's why `UITableViewCell`s event got cancelled. However, i don't understand the differences between these two, subclassing and category, and why does category cancelled events on `UITableViewCell` while subclassing doesn't. – Pham Hoan Dec 09 '13 at 06:46
  • hi @PhamHoan, category vs subclass is already answered in here http://stackoverflow.com/a/522942/2301271 – Joshua Dec 09 '13 at 07:02
  • OK, sorry, I forgot to mark you answer. For people who will have the same problem as mine. Follow Josh's answer and implement `[scrollViewWillBeginDragging:]`. You will be able to dismiss keyboard by touch or drag on UITableView. – Pham Hoan Dec 09 '13 at 07:20