iPhone-在 UITextField 上辞去第一响应者后,无法重新聚焦它

发布于 2024-11-25 13:12:30 字数 759 浏览 1 评论 0原文

我有一个模式窗口,用于从远程服务器搜索数据 - 它有一个 UITextField 作为窗口导航栏的 titleControl,以及一个填充窗口的表格视图(明显显示结果)。现在我想做的是,当用户滚动表格视图时,立即让文本字段失去焦点(放弃第一响应者),以便键盘关闭并且用户有更多空间滚动表格视图(它向下延伸以填充左侧的间隙)通过键盘)。基本上与使用 UISearchDisplayController (或任何它的名称)时的功能相同。

所以我有这段代码用于检测表视图的滚动事件:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [searchField resignFirstResponder];
}

效果很好。然而,问题是,一旦用户滚动表格并且文本字段失去焦点,您就无法通过再次点击它来将焦点重新赋予它。因此,基本上一旦我调用 [resignFirstResponser],我就再也无法恢复键盘并编辑文本字段值。有人知道为什么吗?我是否需要在某个字段上显式调用 [becomeFirstResponder] ?因为我认为这是在点击字段时自动处理的?

另外值得注意的是,当第一次调用模式窗口时,我在文本字段上调用 ​​[becomeFirstResponder] ,因此该字段已预先聚焦。这可能有什么关系吗?

如果有人愿意,我可以发布更多代码,但我不认为我对文本字段做了任何不寻常的事情。

感谢您的帮助!

I have a modal window that's used for searching data from a remote server- it has a UITextField as the titleControl of the navbar for the window, and a tableview filling the window (that displays the results obviously). Now what I want to do is when the user scrolls the tableview, immediately have the textfield lose focus (resign first responder) so that the keyboard dismisses and the user has more room to scroll through the tableview (it stretches down to fill the gap left by the keyboard). Basically the same functionality as when using a UISearchDisplayController (or whatever it's called).

So I have this code for detecting the scroll event of the tableview:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [searchField resignFirstResponder];
}

Which works fine. However, the issue is that once the user scrolls the table and the textfield loses focus, you can't give focus back to it by tapping on it again. So basically once I call that [resignFirstResponser] I can never again bring the keyboard back up and edit the textfield value. Anyone have any idea why? Do I need to explicitly call [becomeFirstResponder] on the field somewhere? Because I thought that was handled automatically when the field is tapped?

Also of note- I am calling [becomeFirstResponder] on the text field right when the modal window is first called up, so the field is pre-focused. Could that have anything to do with it?

I can post more code if anyone would like, but I don't think I'm doing anything out of the ordinary with the textfield.

Thanks for any help!

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

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

发布评论

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

评论(3

紅太極 2024-12-02 13:12:30

您正在从一个函数调用 resignFirstResponder,每次滚动 UIScrollview 时都会调用该函数。因此它不会出现。当 uitextview 失去焦点时,您需要调用 resign。

您可以执行以下操作。这是一个黑客:

  1. 每当你专注于 UITextField 时,创建一个不可见的按钮来覆盖你的滚动视图。
  2. 捕获按钮按下事件并退出第一响应者
  3. 每当 uitextfield 成为第一响应者时创建按钮

这样您将消除错误,即调用 scrollViewWillBeginDragging 中的方法。
其他选项是覆盖 uiTextFieldviewDidAppear 方法。

或者您可以将文本字段放入不同的容器中,并通过检查哪个滚动视图发送消息来处理scrollViewWillBeginDragging。

You are calling the resignFirstResponder from a function which will be called everytime you scroll the UIScrollview. Hence it does not appear. You need to call resign when the uitextview goes out of focus.

You can do the following. Its a hack:

  1. Whenever you focus on the UITextField create a invisible button to overlay your scroll view.
  2. Capture the button press event and resign first responder
  3. Whenever the uitextfield becomes first responder create the button

This way you will remove the bug, viz calling the method in scrollViewWillBeginDragging.
Other option would be to overrite viewDidAppear method for the uiTextField.

Or you could put your textfield into a different container and handle scrollViewWillBeginDragging by checking which scrollview sent the message.

叹倦 2024-12-02 13:12:30

您是否为 searchField 设置了委托?我有同样的问题。我弹出一个模型视图,并将文本字段设置为 viewDidLoad 内的第一响应者。第一次一切都运转良好。但是一旦我关闭模态视图控制器并重新打开它。我的文本字段无法再聚焦。

我发现它与 UITextFieldDelegate 的方法有关。一旦我删除了方法的实现,

– textFieldShouldEndEditing:
– textFieldDidEndEditing:

一切都会正常进行。但不知道为什么

Did u set a delegate for you searchField? I had the same issue. I popup a model view, and set the text field to be the first responder inside viewDidLoad. Everything works well for the first time. But once I dismiss the modal view controller, and reopen it. my text field cannot be focused anymore.

I found it has something to do with methods of UITextFieldDelegate. Once I remove implementation for methods

– textFieldShouldEndEditing:
– textFieldDidEndEditing:

everything works well. but don't know why

攀登最高峰 2024-12-02 13:12:30

您是否正在使用“textFieldShouldEndEditing”进行任何操作,例如@fengd?

我遇到的一个问题是,我正在查看模式视图,而我的“textFieldShouldEndEditing”例程在特定文本字段上错误地返回“NO”。当我的模式被关闭时,我将无法点击任何其他文本字段,大概是因为旧文本字段仍然是“第一响应者”。由于它永远无法结束编辑,因此它会弄乱其后的所有其他文本字段。

我意识到这是两年后的事了,但也许其他人可能会发现这很有用。

Are you doing anything with "textFieldShouldEndEditing", like @fengd?

A problem that I had was that I was viewing a modal view, and my "textFieldShouldEndEditing" routine was incorrectly returning "NO" on a specific text field. When my modal got dismissed, I would be unable to tap on any other text-field, presumably because the old text field was still "first responder". Since it can never end editing, it fouls up all other text fields that come after it.

I realize this is 2 yrs after the fact, but maybe someone else might find this useful.

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