Dismiss Keyboard when user touches outside TextView [duplicate] - ios

This question already has answers here:
iOS - Dismiss keyboard when touching outside of UITextField
(38 answers)
Closed 6 years ago.
I am using this method but keyboard isn't hiding.
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[_myTextView resignFirstResponder];
}
How can I dismiss the keyboard on simply touching outside the keyboard. Would be great if the solution is for both objective c and swift. Thanks
I have a query in objective C for textView inside scrollView. Please resolve. I've searched but have found no discrete solution.

try this
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
[self.yourScrollviewName endEditing:YES];
}
or use like
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if (![[touch view] isKindOfClass:[UITextView class]]) {
[self.view endEditing:YES];
[self.yourScrollviewName endEditing:YES];
}
[super touchesBegan:touches withEvent:event];
}
the above code not work try this
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *resignView = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleSingleTapforResign:)];
resignView.cancelsTouchesInView = NO;
[self.view addGestureRecognizer: resignView];
}
Dismiss what ever is currently editing:
- (void) handleSingleTapforResign:(UITapGestureRecognizer *) sender
{
[self.view endEditing:YES];
[self.yourScrollviewName endEditing:YES];
}

Related

when touches outside the tableview.How to dismiss the tableView? [duplicate]

This question already has answers here:
Hide UITableView on touches outside the tableview
(3 answers)
Closed 6 years ago.
How to dismiss the tableview when we click on the view?i am using the touch began method.but its not working?
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touches began");
UITouch *touch = [touches anyObject];
if(touch.view!=myTableView){
myTableview.hidden = YES;
}
}
how to disable the table when we click on view.i am having three tableviews?
Try this
ViewDidLoad
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handelGesture:)];
[self.view addGestureRecognizer:tap];
ActionMethod
- (void) handelGesture:(UITapGestureRecognizer*)sender
{
myTableview.hidden = YES;
}
Hope this helps.
Please check this code it working if you modified for your requirement Thanks
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSArray *subviews = [self.view subviews];
for (id objects in subviews) {
if ([objects isKindOfClass:[UITextField class]]) {
UITextField *theTextField = objects;
if ([objects isFirstResponder]) {
[theTextField resignFirstResponder];
}
}
}
}

unrecognized selector sent to instance resignFirstResponder

I'm using TPKeyboardAvoiding in my app to hide move text fields when the keyboard is showing, but I'm getting an exception when I try to end editing the text field. It's coming from this method in TPKeyboardAvoiding:
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UIView* view =[self TPKeyboardAvoiding_findFirstResponderBeneathView:self];
NSLog(#"%#",[view description]);
[view resignFirstResponder]; //this line gives the exception
[super touchesEnded:touches withEvent:event];
}
I'm a bit confused here. Shouldn't all UIViews respond to resignFirstResponder? Thanks for the help.
Full error:
2014-03-25 17:40:39.919 Rysk[5553:70b] -[MenuViewController textFieldDidBeginEditing:]: unrecognized selector sent to instance 0xb63c820
Not sure if you have called [yourTextField resignFirstResponder] as well. So it is possible that the UITextField (in the code you have provided) is not the FirstResponder at that point. I would suggest to adjust your code like this:
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UIView* view =[self TPKeyboardAvoiding_findFirstResponderBeneathView:self];
if([view conformsToProtocol:#protocol(UITextFieldDelegate)] || [view conformsToProtocol:#protocol(UITextViewDelegate)]) &&
[view isFirstResponder] && [view canResignFirstResponder])
{
[view resignFirstResponder];
}
[super touchesEnded:touches withEvent:event];
}
Also, if you using PODs please make sure you are using a latest version, because the one i am using has something like this in this event:
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[[self TPKeyboardAvoiding_findFirstResponderBeneathView:self] resignFirstResponder];
[super touchesEnded:touches withEvent:event];
}
Hope it helps!
Update your code as follows:
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UIView* view =[self TPKeyboardAvoiding_findFirstResponderBeneathView:self];
NSLog(#"%#",[view description]);
if([view isKindOfClass:[UITextField class]]) {
UITextField *myTextField = (UITextField *)view;
[myTextField resignFirstResponder];
}
[super touchesEnded:touches withEvent:event];
}
Try this simple method....
- (void)viewDidLoad
{
[super viewDidLoad];
//--Gesture to resign keyborad on touch outside
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
tap.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tap];
}
//-- Resign keyboard on touch UIView
-(void)dismissKeyboard
{
self.view.frame = CGRectMake(self.view.frame.origin.x, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view endEditing:YES];
}
I solved the problem by having my view controller than contains the TPKeyboardAvoidingScrollView implement the UITextFieldDelegate protocol. Most importantly, these two methods:
- (void)textFieldDidBeginEditing:(UITextField *)textField{}
- (void)textFieldDidEndEditing:(UITextField *)textField{}

Two UITextFields With same Hiding Method

Hello I'm trying to have two of my UITextFields' keyboards hide when the user clicks off of the keyboard. I need both of my textfields to have the same method. How can I do this without duplicating them and giving Xcode and error?
Thanks!
Here's the method I'm using for my first textfield I need the same for my other.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([text1 isFirstResponder] && [touch view] != text1) {
[text1 resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
}
Apple strongly recommends to use gesture recognizers, not touchesBegan and other "touches" methods if you do not really have to use them.
Try this code. It will work for any numbers of text views inside view controller.
- (void)viewDidLoad
{
[super viewDidLoad];
UIGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(onBackgroundTapped)];
[self.view addGestureRecognizer:tapRecognizer];
... another initialization code
}
- (void)onBackgroundTapped
{
[self.view endEditing:YES];
}

Most elegant way to dismiss number keyboard for a UITextField on a UiTableView

I have a UITextField on a UITableView and I am using a number keyboard however I want it to be dismissed when user clicks on anything but the UiTextField.
I have seen several solutions however there appears to not be one definitive answer. For example some talk about gestures and when I implement them they do not appear to work using the code below:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[[self view] endEditing:TRUE];
}
As you can see I am trying but there is not one way that seems to be working. Can someone guide me please?
Thanks
Usually, you will want to hit-test the touch against areas which should not dismiss the keyboard; but in general the requirement is to tell the currently-in-focus control to "resign" it's status as "firstResponder". It might look like this:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.userInput resignFirstResponder];
}
However, you may also want to consider a special gesture recognizer for this, so that you are not having to over-analyze the NSSet of touches in the long run (delegate to the GestureRecognizer the task of determining the difference between an actual "dismiss pleas!" tap and a "can I scroll this?" swipe.
You should use resignFirstResponder instead :
[textField resignFirstResponder];
Use the following code
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
// Note the '!':
if(![[touch view] class] isKindOfClass [UITableViewController class]]){
// It's not a bubble they touched, dismiss the keyboard:
[self.view endEditing:YES];
}
[super touchesBegan:touches withEvent:event];
}
or else
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
// Note the '!':
if(![[touch view] class] isKindOfClass [UITableViewController class]]){
// It's not a bubble they touched, dismiss the keyboard:
[textField resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
}
this helps in doing what you want

Detect a touch on a certain object in Xcode

What's the Xcode code for this pseudocode?
if ([Any UIPickerView in my ViewController isTouched]) {
[AnyUIView setHidden:NO];
}
if ([Any UIPickerView in my ViewController is__NOT__Touched__Anymore__]) {
[AnyUIView setHidden:YES];
}
Tried it with the -(void)touchesBeganmethod, it detects the touches but I was not able to make it object-specific. Thanks
P.S. I want to display a hint on the display while the UIPickerViewis touched.
This is just from the top of my head...... but you should be able to get the idea...
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
UITouch *touch = [touches anyObject];
if ([touch.view isKindOfClass: UIPickerView.class]) {
//your touch was in a uipickerview ... do whatever you have to do
}
}
..and do the same with touchesEnded:withEvent:

Resources