Resigning First Responder for multiple UITextField

2019-03-15 06:18发布

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.

13条回答
We Are One
2楼-- · 2019-03-15 07:07

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

查看更多
smile是对你的礼貌
3楼-- · 2019-03-15 07:07

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

查看更多
孤傲高冷的网名
4楼-- · 2019-03-15 07:12

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.
查看更多
Emotional °昔
5楼-- · 2019-03-15 07:15

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

查看更多
走好不送
6楼-- · 2019-03-15 07:16

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];
        }
    }
}
查看更多
甜甜的少女心
7楼-- · 2019-03-15 07:17

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.
}
查看更多
登录 后发表回答