Dismissing keyboard from textfield inside tableviewcell [duplicate] - ios

This question already has answers here:
How to dismiss keyboard iOS programmatically when pressing return
(21 answers)
Closed 7 years ago.
I have a custom tableViewCell which has a textfield inside.
How to dismiss keyboard when tap any place outside the textfields?
Do I have to look through all cells use [cell.textView resignFirstResponder]?

Try this code :
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
}
Or you can add a tap gesture recognizer :
UITapGestureRecognizer *tapper;
- (void)viewDidLoad
{
[super viewDidLoad];
tapper = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleSingleTap:)];
tapper.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapper];
}
- (void)handleSingleTap:(UITapGestureRecognizer *) sender
{
[self.view endEditing:YES];
}

No, you can just use:
[self.view endEditing:YES];
endEditing:
Causes the view (or one of its embedded text fields) to resign the first responder status.
Parameters: force
Specify YES to force the first responder to resign,
regardless of whether it wants to do so.

Related

Dismiss Keyboard in one of multiple views

I have a 3 views in one screen with different components such as tables and buttons, when I try to dismiss the keyboard with the code below it doesn't dismiss when I click on the view that contains the textView( and that’s what I need! ) . However when I click on one of the other views it dismisses.
The following image shows the views that I have and the last view inside the scrollview is the one that contains the textView.
How can I get the keyboard to dismiss despite the view that is taped.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
if(touch.phase == UITouchPhaseBegan) {
[aTextField resignFirstResponder];
}
}
also this code fails to dismiss:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[[self view] endEditing:TRUE];
}
You need to add tap gesture in the views which you want to click:
For you are using two UIViews with name Your_view1 and Your_view2, and textfields are txt1 and txt2. The code is below:
Your_view1:
UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTapGesture:)];
singleTapGesture.numberOfTapsRequired = 1;
[Your_view1 addGestureRecognizer:singleTapGesture];
Your_view2:
UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTapGesture:)];
singleTapGesture.numberOfTapsRequired = 1;
[Your_view2 addGestureRecognizer:singleTapGesture];
And the delegate is:
-(void)handleSingleTapGesture:(UITapGestureRecognizer *)singleTapGesture{
[txt1 resignFirstResponder];
[txt2 resignFirstResponder];
}
If your textView is editable , tapping on that, will make it first responder. That’s why keyboard is not dismissing. If you don’t need edit textView, set it’s editable attribute to false
You should take a look at Apple document for UIScrollView.
Because a scroll view has no scroll bars, it must know whether a touch signals an intent to scroll versus an intent to track a subview in the content. To make this determination, it temporarily intercepts a touch-down event by starting a timer and, before the timer fires, seeing if the touching finger makes any movement.
That's why touchesBegan doesn't work when you click inside your scroll view.
A easy way to fix it, add a UITapGestureRecognizer for your viewController.view.
#property (nonatomic, strong) UITapGestureRecognizer *tapGestureRecognizer;
- (void)viewDidLoad {
[super viewDidLoad];
self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self.aTextField action:#selector(resignFirstResponder)];
[self.view addGestureRecognizer:self.tapGestureRecognizer];
}
managing keyboard get most of our development time luckly we have an alternative solution with this library
No need to write multiple line of code in every viewcontroller simply add one line of code in your didFinishLaunchingWithOptions
IQKeyboardManager.sharedManager().enable = true
This will save time
Hope this will help you

How to remove keyboard from textview when uiview is clicked?

I am trying to remove the keyboard when it is in editing mode of a textview.I have added tap gesture on the main view.But on the click of when editing mode is for textfield then keyboard is removed but when editing mode is for textview then keyboard is not removed.Please tell me how can i tackle this issue?
added tap gesture to the main view.
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleSingleTap:)];
[self.main_view setUserInteractionEnabled:true];
[self.main_view addGestureRecognizer:singleFingerTap];
calling method
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
if([self.txt_username isFirstResponder])
{
[self.txt_username resignFirstResponder];
}
if([self.txt_password isFirstResponder])
{
[self.txt_password resignFirstResponder];
}
}
I have already set the delegate for text view & also i have added the textview protocol.
delete code for set setUserInteractionEnabled =true , u not need that
and add this in your func
-(void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
[self.view endEditing:YES];
}
or u can use library TPKeyboardAvoiding , set for scrollview..its automatic close keyboard when u tap view..so u not need that UITapGestureRecognizer
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[[self view] endEditing:YES];
}
When there are new or changed touches for a given phase, the app object calls one of these methods. Each method takes two parameters: a set of touches and an event.
For more info Click here
i think you just put only textview resingFirstresponder.
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
[self.txt_username resignFirstResponder];
}
use it.

not getting touches from UIScrollView [duplicate]

This question already has answers here:
UIScrollView touchesBegan
(8 answers)
Closed 9 years ago.
I have subclassed UIScrollView and implemented
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
method in it.
But Nothing happens when click or hold the scroll view!
Edit: I also need to use touchesEnded method
I think UIScrollView has two gesture recognizer. They are responsible for handling touch sequences, so they swallow the touch events.
Use scrollView delegate methods to handle drag gestures or the
touchesShouldBegin:withEvent:inContentView:
method to handle scrollview content touches and
touchesShouldCancelInContentView:
to cancel it.
As alternative you can manipulate the panGesture recognizer of the scrollView to pass the event to the next responser.
In your subclass of UIScrollView override the hitTest method like this:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *result = nil;
for (UIView *child in self.subviews)
if ([child pointInside:point withEvent:event])
if ((result = [child hitTest:point withEvent:event]) != nil)
break;
return result;
}
I think you are using scrollview as a subview.So in that case you can use gesture coz same problem i've face it.
You can do using UITapGestureRecognizer like this ...
-(void)viewDidLoad
{
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
gr.delegate = self;
[self.scrollView addGestureRecognizer:gr];
}
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
{
// do here whatever you wanna ..........
}
Solved!
Used LongPressedGestureRecognizer. The action method keeps getting called till the user holds the view. From that i can figure out the state of the gestureRecognizer (UIGestureRecognizerStateBegan, ...Ended, ...Cancelled)

UITableView - resign first responder on outside touch

I have a UITableView with an associated UITableViewController. However, I've modified the table to also have a view with a textfield subview.
As always, I want the keyboard to disappear when the user hits 'done' (easy) and when they touch anywhere else on screen other than the textfield (hard, stuck!).
The normal way to achieve this is to change the class to UIControl so it can handle actions... but I can't do this for my UITableView/UITableViewController combination.
How can I solve this problem?
U can handle user touches by adding a UITapGestureRecognizer to your view.
For example if u don't want to enable row selection in your tableView u call self.tableView.allowsSelection = NO;
But if u still want to detect user touches u add a UITapGestureRecognizer to your tableView (or to tableView.superview).
U can have more control if u implement the UIGestureRecognizerDelegate, this way u can detect and then choose witch touches to receive and witch not.
To do that just add this code to your UITableViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.allowsSelection = NO;
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(viewTapped:)];
tgr.delegate = self;
[self.tableView addGestureRecognizer:tgr]; // or [self.view addGestureRecognizer:tgr];
[tgr release];
}
- (void)viewTapped:(UITapGestureRecognizer *)tgr
{
NSLog(#"view tapped");
// remove keyboard
}
// this is optional, it let u choose witch touches to receive, for example here I'm checking if user has tapped on a textField
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isKindOfClass:[UITextField class]]) {
NSLog(#"User tapped on UITextField");
}
return YES; // do whatever u want here
}
A normal practice is to put a custom UIButton( it becomes visible only when uitextfield begins editing ) behind keyboard view, and when user clicks on screen he actually clicks on that button, and associated selector can resign first responder.
-(void) closeKeyboard:(UIButton *) b {
[self.view endEditing:YES]; //assuming self is your top view controller.
[b setHidden:YES];
}
Using endEditing is better, cause it loops through all subviews and looks for current first responder.
Using alloc breaks with ARC enabled
Just add the following to the viewController
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//where text field is a #property (nonatomic,retain) IBOutlet to your textfield
[textField resignFirstResponder];
}
When a row is tapped the didSelectRowAtIndexPath is called. If a textField located inside inside a row is tapped then the textfield delegate is called.
So in addition to your done button method, in didSelectRowAtIndexPath add a check for the text field being first responder and ask it to resign. Assuming a the selected indexPath is not the row of the textfield.

UITapGestureRecognizer and making the keyboard disappear when tapping background

I created a form and the keypad (Numeric only) appears when entering data like your age.
I want the keyboard to disappear when the user taps the background and I want to add a "Done" button in the empty slot under the 7 (next to the zero). (im using the Number Pad keyboard)
I found this example but I have a few questions.
In
-(void)viewDidLoad
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)]; [self.view addGestureRecognizer:tap];
}
-(void)dismissKeyboard
{
[aTextField resignFirstResponder];
[aTextField1 resignFirstResponder];
[aTextField2 resignFirstResponder];
[aTextField3 resignFirstResponder];
}
If I have more than 1 text field in my form.
Will I need to write every textfield in the dismissKeyboard method?
Easy way to do this is to use the method provided in UIView
- (BOOL) endEditing:(BOOL)force;
This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set to YES, the text field is never even asked; it is forced to resign.
So just do this:
-(void)dismissKeyboard {
[self.view endEditing:YES];
}
and it will support any more text fields you add on your page (under that UIView of course)
You should only send dismissKeyboard to that textField that you are currently editing.
In your code you have got memory leak. Better use this one:
-(void)viewDidLoad
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
[tap release];
}
To check if UITextField is currently in edit mode you can check its property:
A Boolean value indicating whether the text field is currently in edit mode. (read-only)
#property(nonatomic, readonly, getter=isEditing) BOOL editing
For example, you have 3 text fields then dismissKeyboard will look something like this:
-(void)dismissKeyboard
{
UITextField *activeTextField = nil;
if ([textField1 isEditing]) activeTextField = textField1;
else if ([textField2 isEditing]) activeTextField = textField2;
else if ([textField3 isEditing]) activeTextField = textField3;
if (activeTextField) [activeTextField resignFirstResponder];
}
I use the same functionality in many of my apps. Rather than using the GestureRecognizer, I set my view up as a UIControl, rather than a UIView. You can still do the things you'd do with a UIView, but you can also assign IBActions to be performed when interacting with the view.
Here's how to do it:
In Interface Builder, select your view. Then, assign its class to UIControl. (It's probably set up as UIView currently.
In your ViewController for that view, write an IBAction method to detect backgroundTaps. Mine looks like this:
- (IBAction)backgroundTap:(id)sender
{
if ([textField1 isEditing]) {
[textField1 resignFirstResponder];
} else if ([textField2 isEditing]) {
[textField2 resignFirstResponder];
}
}
Finally, in Interface Builder, connect the IBAction you created to the UIControl.
Read this article, it may help you
Writing iOS 4 Code to Hide the iPhone Keyboard (Xcode 4)
Here i give common text field object. and asign reference to it in "textFieldShouldBeginEditing" method. that will common for all text field..
In this case, you need to dismiss one text field that will hide keyboard..
declare textfield object in .h file.
#interface RootViewController : UIViewController
{
IBOutlet UITextField* txt_common;
}
IN .m file
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
[tap release];
}
-(void)dismissKeyboard
{
NSLog(#"hi");
[txt_common resignFirstResponder];
}
#pragma mark TextField methods
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
NSLog(#"hi++++++++++++++++++");
txt_common=textField;
return YES;
}

Resources