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

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

Related

dismiss keyboard when the textfield does not have IBOutlet

How can I dismiss the keyboard in iOS when the textfield does not have an IBOutlet to the view controller? My case is a UITableView with dynamic prototype cells. One of those cells contains a UITextField, however I cannot add an IBOutlet because outlets are not allowed in repeating content.
So how can I achieve to dismiss the keyboard when textfield does not have outlet?
Add any one method in your ViewController.m file :
Choice -1
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
Choice -2
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(resignFieds)];
[self.view addGestureRecognizer:tap];
- (void)resignFieds {
//choice - 1 , check the all subviews and resign textfield
for (UIView * txt in self.view.subviews){
if ([txt isKindOfClass:[UITextField class]] && [txt isFirstResponder]) {
[txt resignFirstResponder];
}
else
{
[self.view endEditing:YES];
}
}
//choice 2 , no need to check any subviews ,
[self.view endEditing:YES];
Note : use any one choice
}
You can use this:
[view endEditing:YES];
try these..
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(taped:)];
[tap setNumberOfTapsRequired:1];
[self.view addGestureRecognizer:tap];
-(void)taped:(UITapGestureRecognizer*)gesture
{
[self.view endEditing:YES];
}
OR
you can do these also..
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
if (![[touch view] isKindOfClass:[UITextField class]])
{
[self.view endEditing:YES];
}
[super touchesBegan:touches withEvent:event];
}
i hope it helps..
Try this :-
In Swift:-
UIApplication.sharedApplication().sendAction(Selector("resignFirstResponder"), to: nil, from: nil, forEvent: nil)
In Objective-C:-
[[UIApplication sharedApplication] sendAction:#selector(resignFirstResponder) to:nil from:nil forEvent:nil];
Explanation:-
We can send an action to the first responder by calling sendAction:to:from:forEvent on the UIApplication singleton and passing nil as the target and in your case your first responder is your textfield.
I assume that you’ve created custom dynamic prototype cell which have UITextField in it.
No need to have IBOutlet of UITextField in your ViewController class where you have added UITableView.
Create Custom TableViewCell Class, then create IBOutlet of UITextField in Custom TableViewCell Class.
#interface SimpleTableViewCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UITextField *simpleTxtField;
#end
Set TableViewCell Class to Custom TableViewCell class say “SimpleTableViewCell”. by selecting TableViewCell and selecting Identity Inspector icon in Right side pane in Xcode.
in your ViewController class confirm to UITextField Delegate. like this.
#interface ViewController () <UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate>
#property (weak, nonatomic) IBOutlet UITableView *simpleTableView;
#end
implement UITextField delegate Method.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
then in tableView:cellForRowAtIndexPath method of UITableView Datasource set your Custom TableViewCell’s (SimpleTableViewCell) TextField Delegate to self.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
SimpleTableViewCell *cell = [self.simpleTableView dequeueReusableCellWithIdentifier:#"simpleCell" forIndexPath:indexPath];
cell.simpleTxtField.delegate = self;
return cell;
}
and whenever you select TextField of Cell a Keyboard will display and when you click return key a TextField Delegate method will get call and Keyboard will get dismiss.
Hope this will help.

get CGPoint inside didSelectItemAtIndexPath method of CollectionView

Is there a way that I can get coordinates of clicked point inside collectionViewCell? I want to do method A if I clicked at rect with Y coordinate < 50, and method B if Y >50.
There is also option B, to subclass the UITableViewCell and get the location from the UIResponder class:
#interface CustomTableViewCell : UITableViewCell
#property (nonatomic) CGPoint clickedLocation;
#end
#implementation CustomTableViewCell
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
UITouch *touch = [touches anyObject];
self.clickedLocation = [touch locationInView:touch.view];
}
#end
Then get the location from the TableViewCell it self:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//get the cell
CustomTableViewCell *cell = (CustomTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
//get where the user clicked
if (cell.clickedLocation.Y<50) {
//Method A
}
else {
//Method B
}
}
Assuming you have a custom UICollectionViewCell, you can add a UITapGestureRecognizer to the cell and get the touch point in the touchesBegan handler. Like this:
//add gesture recognizer to cell
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] init];
[cell addGestureRecognizer:singleTap];
//handler
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
if (touchPoint.y <= 50) {
[self methodA];
}
else {
[self methodB];
}
}

didSelectRowAtIndexPath not being called because of UITapGestureRecognizer

I'm using UITapGestureRecognizer to end editing (because that's the workaround I found useful to end editing on input keyboards that have no other way to do so, like the Decimal Pad). The problem is that inside that viewController I have a tableView (the UITableViewDataSource and UITableViewDelegate of that tableView are set to the viewController) and the method didSelectRowAtIndexPath is not being triggered.
Code:
viewDidLoad {
...
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tap:)];
[self.view addGestureRecognizer:tapRecognizer];
...
}
- (void)tap:(UIGestureRecognizer*)gr {
[self.view endEditing:YES];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Help!");
}
I know the UITapGestureRecognizer is catching the selection, because if I comment out as following:
viewDidLoad {
...
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tap:)];
//[self.view addGestureRecognizer:tapRecognizer];
...
}
now the method the didSelectRowAtIndexPath finally triggers out.
I need help with some good practices on how to workaround the "endEditing" or how to forward the tap gesture to the tableView so that the didSelectRowAtIndexPath triggers out.
Thanks!
I assume that you haven't a tableView that takes all the screen, otherwise you can close the keyboard in didSelectRowAtIndexPath.
Any way, you can do your stuff and the pass the event to the next responder (that is kind of class UITableViewCell if there is, so if user tap on the tableView):
- (void)tap:(UIGestureRecognizer*)gr {
[self.view endEditing:YES];
UIResponder *responder = self;
while (responder.nextResponder != nil){
responder = responder.nextResponder;
if ([responder isKindOfClass:[UITableViewCell class]]) {
break;
}
}
[responder touchesBegan:touches withEvent:event];
}

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.

IOS: hide keyboard on touch UITableView

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

Resources