Naresh
Naresh

Reputation: 99

Hide Keyboard when user taps outside the text field in UITableView and UIScrollView

My UIViewController hierarchy is as follows

UIView
    UIScrollView
        UITableView
            UITableViewCell
                UITextField

The UITableView is added to the view controller programmatically. I want to hide keyboard when user taps outside UTTextField either on the view or on the UITableView I am executing some methods when user taps on other UITableView rows

I tried

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

UIScrollView doesn't send the touch events.

I tried adding Tap gesture

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[singleTap setNumberOfTapsRequired:1];

[[self view] addGestureRecognizer:singleTap];

but with TapGesture, hides the following event

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

are there any other possible ways to hide the keyboard?

Upvotes: 1

Views: 3466

Answers (5)

Boobalan
Boobalan

Reputation: 823

UITableView didSelectRowAtIndexPath will not call when UITableview is Edit Mode. So you suppose to create custom gesture event to handle the same.

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//cell design code goes here.
 UITapGestureRecognizer *doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
doubleTapGestureRecognizer.numberOfTapsRequired = 1;
//tapGestureRecognizer.delegate = self;
[cell addGestureRecognizer:doubleTapGestureRecognizer];
 return cell; 
} 
//Handle the click event 
-(void) handleDoubleTap:(UITapGestureRecognizer*)sender{ 
[self.view endEditing:YES]; 
UITableViewCell * cell =(UITableViewCell*) sender.view; 
//get the selected table indexpath. 
NSIndexPath * indexPath= [tblCart indexPathForCell:cell]; //to handle the scroll
tblCart scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; 
NSLog(@"Comming"); 
}

Upvotes: 0

GilroyKilroy
GilroyKilroy

Reputation: 894

If you want to put a gesture recognizer on the background view you need to make sure it has one.

Add

self.tableView.backgroundView = [[UIView alloc] initWithFrame:self.tableView.bounds];

Upvotes: 0

NiKKi
NiKKi

Reputation: 3296

use the code : [self.view endEditing:YES];

Upvotes: 2

Saad
Saad

Reputation: 8947

use the UITextFieldDelegate and the method

– textFieldShouldEndEditing:(UITextField*) txtField
{

[txtField resignKeyPads];
return YES:
}

this can also done by the scrolview delgate too

-(void) scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    //resign all keypads of all textfields use array containing keypads
}

one thing more is change the class of UIView to UIControl and make a method IBAction and connect the UIControl touchupInside to that ibaction, it will resign keypads

Upvotes: 1

shoughton123
shoughton123

Reputation: 4251

If you want to still use tap gestures you need to add the gesture recogniser to the table background like so:

[tableView.backgroundView addGestureRecognizer:singleTap];

This will prevent the hiding of:

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

Upvotes: 0

Related Questions