简体   繁体   English

为多个 UITextFields 辞职第一响应者

[英]Resigning First Responder for multiple UITextFields

There is an application in which I am generating multiple UITextFields dynamically.有一个应用程序,我在其中动态生成多个UITextFields I want to resign first responder whenever the UITextFields are not selected (touch outside the UITextField ).每当未选择UITextFields (触摸UITextField外部)时,我想辞去第一响应者的职务。 How can I know that of which UITextField I have to resign first responder?我怎么知道我必须辞去哪个UITextField第一响应者? Please specify any other way beyond the 'tag' concept because I have tried that.请指定“标签”概念之外的任何其他方式,因为我已经尝试过了。 Please suggest the right direction.请提出正确的方向。 Thanks in advance.提前致谢。

Don't call resignFirstResponder ;不要调用resignFirstResponder call endEditing: !呼叫endEditing:

Call endEditing: on any view above the text fields in the view hierarchy.调用endEditing:在视图层次结构中文本字段上方的任何视图上。 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).使用endEditing:YES强制它或endEditing:NO让文本字段的委托决定它是否应该结束编辑(如果您正在验证输入很有用)。

**[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;因为你可以采取 currentTextField = textField;

in another delegate method在另一个委托方法中

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

you can do currentTextField = nil;你可以做 currentTextField = nil;

you can then resign currentTextField....然后你可以辞职 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:UITableViewController中最通用的方式也是唯一对我有用的方式是将动作发送到响应者链并等待正确的 object 接收动作:

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

Tested in iOS8在 iOS8 中测试

Using [self.view endEditing:YES];使用[self.view endEditing:YES]; to resignFirstResponder for the current active UITextField.为当前活动的 UITextField resignFirstResponder。

This worked for me in Xamarin.iOS / Monotouch.这在 Xamarin.iOS / Monotouch 中对我有用。 Change the keyboard button to Next, pass the control to the next UITextField and hide the keyboard after the last UITextField.将键盘按钮更改为 Next,将控件传递给下一个 UITextField 并在最后一个 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:ViewDidLoad 中,您将拥有:

If your TextFields haven't a Tag set it now:如果您的 TextFields 现在还没有设置标签:

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.我在为从多个 TextField 中选择的 UITextField辞职第一响应者时遇到问题。 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];
        }
    }
}

You can set textFields as properties and synthesize them.您可以将textFields设置为属性并合成它们。 You will need as many properties as you are using in your app.您将需要在您的应用程序中使用的尽可能多的属性。

You can have @synthesise myTextField0, myTextField1, myTextField2;你可以有@synthesise myTextField0, myTextField1, myTextField2; for three textFields.三个文本字段。 Just assign each UITextField you are using to these properties, while declaring them.只需将您正在使用的每个UITextField分配给这些属性,同时声明它们。

And when you want to resign them, just use [self.myTextField0 resignFirstResponder] at textFieldDidEndEditing or which ever function you want to resign them.当你想辞职时,只需在textFieldDidEndEditing或 function 中使用[self.myTextField0 resignFirstResponder]即可让他们辞职。 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;通常,您可以跳过所有这些步骤,使用textField.returnKeyType = UIReturnKeyDone; if you have a DONE return key, you can just go to the method如果你有一个DONE返回键,你可以只 go 到方法

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

or you can use tags to tag certain textFields and then resign particular ones.或者您可以使用tags来标记某些文本字段,然后退出特定的。

you can check with isFirstResponder .你可以检查isFirstResponder At any point of time only one UIResponder ( UITextField in your case) can be firstResonder .在任何时候,只有一个UIResponder (在您的情况下为UITextField )可以是firstResonder

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

You can use endEditing instead of resignFirstResponder您可以使用endEditing而不是resignFirstResponder

Try This尝试这个

[self.view EndEditing:YES]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM