14

There is an application in which I am generating multiple UITextFields dynamically. I want to resign first responder whenever the UITextFields are not selected (touch outside the UITextField). How can I know that of which UITextField I have to resign first responder? Please specify any other way beyond the 'tag' concept because I have tried that. Please suggest the right direction. Thanks in advance.

rpax
  • 4,306
  • 6
  • 29
  • 53
Kritanshu_iDev
  • 454
  • 1
  • 6
  • 8
  • possible duplicate of [Easy way to dismiss keyboard?](http://stackoverflow.com/questions/741185/easy-way-to-dismiss-keyboard) – Loukas Nov 14 '14 at 11:18

13 Answers13

51

Don't call resignFirstResponder; call endEditing:!

Call endEditing: on any view above the text fields in the view hierarchy. It will locate the first responder and ask it to resign. Use endEditing:YES to force it or endEditing:NO to let the text field's delegate decide if it should end editing (useful if you are validating input).

benzado
  • 74,658
  • 20
  • 105
  • 133
8
**[self.view endEditing:TRUE];** //Resign firstresponder for all textboxes on the view
Nishant
  • 11,895
  • 9
  • 53
  • 89
3

use this code and implement

-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
  NSInteger nextTag = textField.tag + 1;
  // Try to find next responder
  UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
  if (nextResponder) {
    // Found next responder, so set it.
    [nextResponder becomeFirstResponder];
  } else {
    // Not found, so remove keyboard.
    [textField resignFirstResponder];
  }
  return NO; // We do not want UITextField to insert line-breaks.
}
Deepesh
  • 655
  • 4
  • 11
3

You can implement delegate method

- (void)textFieldDidBeginEditing:(UITextField *)textField; 

in that you can take currentTextField = textField;

in another delegate method

- (BOOL)textFieldShouldReturn:(UITextField *)textField; 

you can do currentTextField = nil;

you can then resign currentTextField....

iMOBDEV
  • 3,567
  • 2
  • 19
  • 33
3

You can try it like this:

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

    for (id textField in self.view.subviews) {

        if ([textField isKindOfClass:[UITextField class]] && [textField isFirstResponder]) {
            [textField resignFirstResponder];
        }
    }
} 

I didn't try it but it seems a good solution

Frade
  • 2,780
  • 4
  • 25
  • 37
1

The most generic way and also the only one that worked for me in an UITableViewController was sending the action down the responder-chain and wait for the right object to receive the action:

[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];

Tested in iOS8

Julian F. Weinert
  • 7,076
  • 6
  • 56
  • 100
0

Using [self.view endEditing:YES]; to resignFirstResponder for the current active UITextField.

Kit
  • 2,120
  • 11
  • 11
0

This worked for me in Xamarin.iOS / Monotouch. Change the keyboard button to Next, pass the control to the next UITextField and hide the keyboard after the last UITextField.

private void SetShouldReturnDelegates(IEnumerable<UIView> subViewsToScout )
{
  foreach (var item in subViewsToScout.Where(item => item.GetType() == typeof (UITextField)))
  {
    (item as UITextField).ReturnKeyType = UIReturnKeyType.Next;
    (item as UITextField).ShouldReturn += (textField) =>
    {
        nint nextTag = textField.Tag + 1;
        var nextResponder = textField.Superview.ViewWithTag(nextTag);
        if (null != nextResponder)
            nextResponder.BecomeFirstResponder();
        else
            textField.Superview.EndEditing(true); 
            //You could also use textField.ResignFirstResponder(); but the above line makes some users happier (e.g. benzado)

        return false; // We do not want UITextField to insert line-breaks.
    };
  }
}

Inside the ViewDidLoad you'll have:

If your TextFields haven't a Tag set it now:

txtField1.Tag = 0;
txtField2.Tag = 1;
txtField3.Tag = 2;
//...

and just the call

SetShouldReturnDelegates(yourViewWithTxtFields.Subviews.ToList());
//If you are not sure of which view contains your fields you can also call it in a safer way:
SetShouldReturnDelegates(txtField1.Superview.Subviews.ToList());
//You can also reuse the same method with different containerViews in case your UITextField are under different views.
Daniele D.
  • 2,416
  • 3
  • 34
  • 42
0

I had a problem in Resigning first responder for the selected UITextField from the multiple TextField. I find out this is working for me after so many different solutions.

- (void)textFieldDidBeginEditing:(UITextField *)textField
{

    for (id textField in self.view.subviews) {

        if ([textField isKindOfClass:[UITextField class]] && [textField isFirstResponder] && [textField isEqual:_selectedLabel] ) {
            [textField resignFirstResponder];
        }
    }
}
0

You can set textFields as properties and synthesize them. You will need as many properties as you are using in your app.

You can have @synthesise myTextField0, myTextField1, myTextField2; for three textFields. Just assign each UITextField you are using to these properties, while declaring them.

And when you want to resign them, just use [self.myTextField0 resignFirstResponder] at textFieldDidEndEditing or which ever function you want to resign them. And you can use this for other textFields also. This is the way to handle multiple textFields

In general, you can skip all these steps, with textField.returnKeyType = UIReturnKeyDone; if you have a DONE return key, you can just go to the method

 - (BOOL) textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return 1;
}

or you can use tags to tag certain textFields and then resign particular ones.

Legolas
  • 11,767
  • 11
  • 73
  • 129
  • thanks for response but the matter was of generating UITextField dynamically..so was not able to know how many will be there? So as also was not able to synthesize and so things... Although have done in different manner !! Thanks!! – Kritanshu_iDev Jul 15 '11 at 09:47
0

you can check with isFirstResponder. At any point of time only one UIResponder (UITextField in your case) can be firstResonder.

Loukas
  • 588
  • 1
  • 5
  • 21
Tatvamasi
  • 2,489
  • 16
  • 14
  • Thanks for suggestion!! I have done it using other way but want to know full procedure of yours.. can you post a block of code? Thanks in advance!! – Kritanshu_iDev Jul 15 '11 at 09:44
0

By the way..i have done this in different manner.. Not looking quite a good programming Champ tech but enough to solve my work!!

for(UIView *v in self.view.subviews)
    {
        if(([v isMemberOfClass:[UITextField class]]==YES) && !(CGRectContainsPoint(v.frame,[[touches anyObject] locationInView:self.View)))
           {
               v.userInteractionEnabled=NO;
               if([v isEditing])
               {
               [v resignFirstResponder];
               }
           }
    }
Toastor
  • 8,890
  • 4
  • 47
  • 79
Kritanshu_iDev
  • 454
  • 1
  • 6
  • 8
-1

You can use endEditing instead of resignFirstResponder

Try This

[self.view EndEditing:YES]
Kumar KL
  • 15,086
  • 9
  • 36
  • 57
sukumar
  • 65
  • 1
  • 5