IOS: hide keyboard on touch UITableView - ios

I am new in iOS development. I want to hide the keyboard when tapping outside of a UITextView.
My TextView is in a cell from an UITableView. The problem is that I have a Toolbar at the top and my buttons doesn't react anymore. I implemented the method "shouldReceiveTouch" but my test is not correct i think. Any ideas? Thank you and sorry for my bad english..
In my ViewDidLoad:
tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];
tap.delegate = self;
[self.view addGestureRecognizer:tap];
note: tap is an UITapGestureRecognizer property.
Implemented methods:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isKindOfClass:[UIBarButtonItem class]]) {
return NO;
}
return YES;
}
-(void)dismissKeyboard {
[tview resignFirstResponder];
}

UIBarButtonItem is not a subclass of UIView, hence the shouldReceiveTouch still return YES.
Try to exclude the whole UIToolbar or just add the tap gesture recognizer in the UITableViewCell when you initialize the cell in cellForRowAtIndexPath.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isKindOfClass:[UIToolbar class]]) {
return NO;
}
return YES;
}

You should add your gesture to table view.
tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];
tap.delegate = self;
[tblView addGestureRecognizer:tap];

use didScroll method of UIScrollView delegate to resign keyboard.TableView is also subclass of UIScrollView so it should work.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[tview resignFirstResponder];
}
or use this one
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[tview resignFirstResponder];
}
If you still want to use gesture then add gesture to UIView or self.view or superView of tableView
instead of adding it to tableView
Try following code :----
Keep you code as it is and add this method
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[tview resignFirstResponder];
}

in viewDidLoad set self.view.userInteractionEnabled = yes;
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([touch view] == tview) {
[tview resignFirstResponder];
}
}

Related

How to Disable Tap Gesture for Scroll view's sub view?

I have created one scrollview for registration. I have also use tableview for drop-down(Male/Female) and tap Gesture on scrollview for hide all input views because touchesBegan method dose not call. whenever i click on table view cell,it will not call didselect method but consider as tap on scrollview.
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
tap.numberOfTapsRequired=1;
[_scrollview_out addGestureRecognizer:tap];
Method:-
-(void)dismissKeyboard
{
[_txt_name resignFirstResponder];
[_txt_mname resignFirstResponder];
[_txt_surname resignFirstResponder];
[_txt_gender resignFirstResponder];
[_txt_dob resignFirstResponder];
_tbl_view_gender.hidden=true;
_txt_dob.hidden=false;
_lbl_dob.hidden=false;
}
Table View didSelect Method:-
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[_scrollview_out endEditing:YES];
str2=[gender_arr objectAtIndex:indexPath.row];
_txt_gender.text=str2;
_tbl_view_gender.hidden=true;
_txt_dob.hidden=false;
_lbl_dob.hidden=false;
}
for me there are two options:
1: add the tableview in scrollView's superView
2: subclass UIScrollView, overwrite method:
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
//if the touch point in the tableview.
//return NO;
//else
//return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isDescendantOfView:Your Tableview]]) {
// Don't let selections of auto-complete entries fire the
// gesture recognizer
return NO;
}
return YES;
}

Objective C - UIPanGestureRecognizer selector not invoked with UISlider

I am trying to using UIPanGestureRecognizer and UITapGestureRecognizer on UISlider. tap is working fine i am facing problem with UIPanGestureRecognizer. I need to call handleTransactionPan method on changing value of UISlider but it is never being called because of its delegate method.
How can i invoke the selector while keeping the delegate methods to fire? Here is the code for what i have done so far.
- (void)viewDidLoad
{
[super viewDidLoad];
pr=[[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handleTransactionPan:)];
pr.delegate=self;
[slider addGestureRecognizer:pr];
tr=[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
tr.delegate=self;
[slider addGestureRecognizer:tr];
}
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return YES;
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if(gestureRecognizer==pr)
{
if ([touch.view isKindOfClass:[UISlider class]]) {
return NO;
}
}
return YES;
}

How to detect UITapGestureRecognizer 's clicked view?

I am using :
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard:)];
[self.view addGestureRecognizer:tap];
in order to close keyboard when clicked anywhere else from UITextField.
However in my view, I have UITableView , and I have to detect when click on the rows of UITableView.
Because of UITapGestureRecognizer my didSelectRowAtIndexPath function is not called. Is there any way to detect whether the clicked object is UITableViewCell?
You can get thouch event of Gesture from
- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
you detect touch of gesture so do logically like bellow:
- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isKindOfClass:[UITextFiled class]])
{
return FALSE;
}
else
{
// here is remove keyBoard code
return TRUE;
}
}
Implement this delegate method gestureRecognizerShouldBegin:, check and cancel your gesture callback which happen on tableview cell(row) and trigger tableview's delegate method.
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer.view isKindOfClass:[UITableViewCell class]])
{
return NO;
}
return YES
}

how to dismiss a keyboard when there is no textfield to resignFirstResponder

in the following scenario clicking on the pencil turns the iz 1 text to be editable by making it becomeFirstResponder and the keyboard opens, I wish to close the keyboard when clicking on the "empty rows" or iz2.
how can i do that?
I've tried adding to the cellView
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self dismissKeyboard];
}
but it didn't work
You can hide keyboard using this:
[self.view endEditing:YES];
EDIT
Add gesture in where you can call becomeFirstResponder.
- (void)showKeyboard
{
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hide)];
[self.tableView addGestureRecognizer:gesture];
}
and remove it in hide method,
- (void)hide
{
[self.view endEditing:YES];
[self.view removeGestureRecognizer:self.view.gestureRecognizers[0]];
}
Just call:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
Or:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSIndexPath *indexPathForYourCell = [NSIndexPath indexPathForRow:0 inSection:0];
YourCustomCell *cell = [self tableView:self.tableView cellForRowAtIndexPath:indexPathForYourCell];
[cell.iz1TextField resignFirstResponder];
}
my final solution was something similar to what #caglar suggested here is the code i've written inside tableviewcontroller:
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
}
-(void)dismissKeyboard
{
[self.view endEditing:YES];
}

IOS detecting touch to dismiss keyboard?

I am trying to figure out how to dismiss the keyboard and trigger a method when the user taps outside of a UITextField
in TableViewCell.m:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[self.delegate cellDidBeginEditing:self];
}
in ViewController.m:
-(void)cellDidBeginEditing:(TableViewCell *)editingCell
{
_editingOffset = _tableView.scrollView.contentOffset.y - editingCell.frame.origin.y;
for (TableViewCell *cell in [_tableView visibleCells]) {
[UIView animateWithDuration:0.3
animations:^{
cell.frame = CGRectOffset(cell.frame, 0, _editingOffset);
if (cell != editingCell) {
cell.alpha = 0.25;
}
}];
}
}
the cellDidBeginEditing: method displaces the cell to the top and shades the other cells grayish.
I have another method, cellDidEndEditing: which does the opposite of this, and the code is not really needed.
As of now, selecting, for example, "cell2" when editing "cell1" just triggers cellDidBeginEditing for "cell2"
I want the keyboard to dismiss and cellDidEndEditing to trigger when I click outside of "cell1"
Try implementing and calling these functions:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesBegan:withEvent");
[self.view endEditing:YES];
[super touchesBegan:touches withEvent:event];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self.firstTextField resignFirstResponder];
[self.secondTextField resignFirstResponder];
return YES;
}
This should make it so that when you touch any where outside of the two textFields, the keyboard automatically dismisses.

Resources