Resigning First Responder for multiple UITextFields

牧云@^-^@ 提交于 2019-12-03 02:14:11

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).

**[self.view endEditing:TRUE];** //Resign firstresponder for all textboxes on the view

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.
}

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....

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

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

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.

Kritanshu_iDev

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

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

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

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.

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

You can use endEditing instead of resignFirstResponder

Try This

[self.view EndEditing:YES]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!