为多个 UITextField 辞职第一响应者

发布于 2024-11-24 06:30:19 字数 202 浏览 5 评论 0原文

有一个应用程序,我在其中动态生成多个 UITextFields。每当未选择 UITextFields 时(触摸 UITextField 外部),我想辞职第一响应者。我如何知道我必须辞去哪个 UITextField 的第一响应者?请指定“标签”概念之外的任何其他方式,因为我已经尝试过。请建议正确的方向。提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

一世旳自豪 2024-12-01 06:30:19

不要调用resignFirstResponder;调用endEditing:

在视图层次结构中文本字段上方的任何视图上调用 endEditing:。它将找到第一响应者并要求其辞职。使用 endEditing:YES 强制执行,或使用 endEditing:NO 让文本字段的委托决定是否应该结束编辑(如果您正在验证输入,则很有用)。

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

北陌 2024-12-01 06:30:19
**[self.view endEditing:TRUE];** //Resign firstresponder for all textboxes on the view
**[self.view endEditing:TRUE];** //Resign firstresponder for all textboxes on the view
随心而道 2024-12-01 06:30:19

您可以实现委托方法

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

,因为您可以采取 currentTextField = textField;

在另一个委托方法中,

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

您可以执行 currentTextField = nil;

然后你可以退出 currentTextField....

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

情绪少女 2024-12-01 06:30:19

使用此代码并实施

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

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.
}
小猫一只 2024-12-01 06:30:19

你可以这样尝试:

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

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

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

我没有尝试过,但这似乎是一个很好的解决方案

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

一杯敬自由 2024-12-01 06:30:19

UITableViewController 中最通用的方法也是唯一对我有用的方法是将操作发送到响应者链并等待正确的对象接收操作:

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

在 iOS8 中测试

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

北陌 2024-12-01 06:30:19

您可以将 textFields 设置为属性并合成它们。您将需要与应用程序中使用的属性一样多的属性。

您可以为三个文本字段使用 @synthesise myTextField0, myTextField1, myTextField2;。只需将您正在使用的每个 UITextField 分配给这些属性,同时声明它们即可。

当您想要放弃它们时,只需在 textFieldDidEndEditing 或您想要放弃它们的任何函数中使用 [self.myTextField0 resignFirstResponder] 即可。您也可以将其用于其他文本字段。这就是处理多个textField的方法。

一般来说,你可以跳过所有这些步骤,用textField.returnKeyType = UIReturnKeyDone;
如果您有 DONE 返回键,则可以直接转到该方法

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

,也可以使用 tags 来标记某些文本字段,然后放弃特定的文本字段。

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.

为你鎻心 2024-12-01 06:30:19

顺便说一句..我用不同的方式做到了这一点..看起来不是一个很好的编程冠军技术,但足以解决我的工作!

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

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];
               }
           }
    }
薄荷梦 2024-12-01 06:30:19

您可以使用isFirstResponder进行检查。
在任何时间点,只有一个 UIResponder(在您的情况下为 UITextField)可以是 firstResonder

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

往日 2024-12-01 06:30:19

使用 [self.view endEditing:YES]; 为当前活动的 UITextField 退出FirstResponder。

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

行雁书 2024-12-01 06:30:19

这在 Xamarin.iOS / Monotouch 中对我有用。
将键盘按钮更改为“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.
    };
  }
}

ViewDidLoad 内,您将拥有:

如果您的 TextFields 没有标签,请立即设置它:

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

并且只​​需调用

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.

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.
止于盛夏 2024-12-01 06:30:19

我在从多个 TextField 中选择的 UITextField 中退出第一响应者时遇到问题。
在经历了这么多不同的解决方案之后,我发现这对我来说有效

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

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

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

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];
        }
    }
}
说不完的你爱 2024-12-01 06:30:19

您可以使用 endEditing 而不是 resignFirstResponder

试试这个

[self.view EndEditing:YES]

You can use endEditing instead of resignFirstResponder

Try This

[self.view EndEditing:YES]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文