告诉任何 UITextField 关闭

发布于 2024-10-15 06:14:15 字数 489 浏览 1 评论 0原文

我如何告诉当前的 firstResponder 辞职?我有一个带有一堆 TextFieldsUITableView ,但我不知道哪个始终处于活动状态。我考虑过将指向数组中所有单元格的指针存储起来并迭代它,告诉每个单元格resignFirstResponder,但我确信有一种更简单的方法。也许类似于[CurrentFirstResponder resignFirstResponder]

我希望得到一些帮助,Fabian

编辑:我不想在用户点击完成时关闭键盘。应该以编程方式将其驳回。由于我不知道哪个 UITextField 随时处于活动状态,因此我正在搜索在当前 FirstResponder 上调用 resignFirstResponder 的内容。

How can I tell the current firstResponder to resign? I have a UITableView with a bunch of TextFields and I don't know which is active at all times. I thought about storing pointers to all cells in an array and iterating through it, telling every cell to resignFirstResponder but I'm sure there is an easier way. Maybe something like [CurrentFirstResponder resignFirstResponder]?

I would appreciate some help, Fabian

EDIT: I don't want to dismiss the keyboard when the user taps done. It should be dismissed programmatically. Since I don't know which UITextField is active at any time, I am searching for something that calls resignFirstResponder on the current FirstResponder.

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

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

发布评论

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

评论(3

-柠檬树下少年和吉他 2024-10-22 06:14:15

您可以使用 UITextFieldDelegate 协议 或者您可以使用父视图执行此操作:

UIView * myParentViewView;//view containing one or more editable UI controls 
[myParentViewView endEditing:YES];

You could keep a reference to the UITextfeild that's actively editing using textFieldDidBeginEditing: on the UITextFieldDelegate Protocol or you could do this with your parent view:

UIView * myParentViewView;//view containing one or more editable UI controls 
[myParentViewView endEditing:YES];
深巷少女 2024-10-22 06:14:15

我希望这能解决您的问题,

将委托分配给 UItextField,

textField.delegate=self;

然后在以下方法中

    - (void)textFieldDidBeginEditing:(UITextField *)textField
    {
//This for to resign on begin editing
    [textField resignFirstResponder];
    }

 - (void)textFieldDidEndEditing:(UITextField *)textField
    {
//This for to resign on end editing
    [textField resignFirstResponder];
    }

如果您不希望文本字段可编辑,则

textField.editing=NO;

设置标记来区分您的文本字段

I hope this will solve your problem,

Assign delegate to UItextField,

textField.delegate=self;

then in following method

    - (void)textFieldDidBeginEditing:(UITextField *)textField
    {
//This for to resign on begin editing
    [textField resignFirstResponder];
    }

 - (void)textFieldDidEndEditing:(UITextField *)textField
    {
//This for to resign on end editing
    [textField resignFirstResponder];
    }

If you dont want to the textField to be editable then,

textField.editing=NO;

Set tag to distingush your textFields

请持续率性 2024-10-22 06:14:15

只需使用 UITextFieldDelegate (参考 )。每当调用 - (BOOL)textFieldShouldReturn:(UITextField *)textField 时,请执行 [textField resignFirstResponder],因为始终使用当前活动的文本字段调用此方法。
如果您仍然需要区分文本字段,请尝试设置标签并将其与 if(textfield.tag == self.mytextfield.tag) {...} 一起使用

Simply use the UITextFieldDelegate (reference). Whenever - (BOOL)textFieldShouldReturn:(UITextField *)textField is called, perform [textField resignFirstResponder], since this method is always invoked with the currently active textfield.
If you still need to distinguish between your textfields, try setting a tag and use it with if(textfield.tag == self.mytextfield.tag) {...}

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