视图出现后 UITextField 丢失 firstResponder [英] UITextField losing firstResponder after view appears

查看:28
本文介绍了视图出现后 UITextField 丢失 firstResponder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIPageViewController.一个页面有一个按钮,另一个页面有一个带有按钮的 UITextField.当页面滚动到带有该字段的视图时,我希望它 becomeFirstResponder 并打开键盘.这是发生的事情:

I have a UIPageViewController. One page has a single button, and the other page has a UITextField with a button. When the page scrolls to the view with the field, I'd like it to becomeFirstResponder and open the keyboard. Here's what happens:

  • 我调用了 [self.locationQuery becomeFirstResponder] ViewController 的 viewDidAppear 方法.但它永远不会打开键盘.

  • I call [self.locationQuery becomeFirstResponder] ViewController's viewDidAppear method. But it never opens the keyboard.

我在 viewWillAppear 中做了同样的事情,它出现的时间很短,但很快就消失了.

I do the same in viewWillAppear, and it appears briefly, but then is quickly dismissed.

如果我在带有文本字段的页面上,将页面拉到一半然后放开(不更改页面),self.locationQuery 会获得焦点.>

If I'm on the page with the text field, and pull the page partway and let it go (without changing pages), self.locationQuery receives focus just fine.

似乎其他东西正在从现场获取 firstResponder 状态,但我最不知道是什么,以及为什么它只会在页面更改时发生(而不是在翻页失败后显示).有什么想法吗?

It seems like something else is grabbing firstResponder status from the field, but I haven't the faintest idea what, and why it would only be happening when the page changed (rather than revealed after a failed page turn). Any ideas?

更新

我创建了一种抓取视图的方法,以查看是否有任何其他视图确实采用了 firstResponder(来自对这个问题的回答:在不使用私有 API 的情况下获取当前的第一响应者).结果:

I created a way to crawl the views to see if any other views were, indeed, taking firstResponder (from an answer to this question: Get the current first responder without using a private API). Results:

  • 当我明确地为文本字段提供第一响应者时,该方法报告它具有第一响应者状态.

  • When I explicitly give first responder to the text field, the method reports it has first responder status.

当我不这样做时,它返回null.

When I don't, it returns null.

现在我更糊涂了.

推荐答案

我不太了解导致我的问题的本质,但我能够通过将调用包装在 viewDidAppear:

I don't really understand the nature of what was causing my issue, but I was able to fix it by wrapping the call in an async dispatch in viewDidAppear:

dispatch_async(dispatch_get_main_queue(), ^{
    MapManualViewController *strongSelf = weakSelf;
    [strongSelf.locationQuery becomeFirstResponder];
});

这篇关于视图出现后 UITextField 丢失 firstResponder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆