xcode becomeFirstResponder 第二次不显示键盘(ios 6 + 故事板)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12768844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:43:44  来源:igfitidea点击:

becomeFirstResponder does not show keyboard second time (ios 6 + storyboard)

iphoneobjective-ciosxcodestoryboard

提问by Jarle Hansen

I have a storyboard, which includes navigation between views. Back and forth, and there is also the possibility to jump all the way to the home screen.

我有一个故事板,其中包括视图之间的导航。来回,也有可能一路跳到主屏幕。

Basically, it works like this:

基本上,它是这样工作的:

  • View 1 shows a simple menu.
  • View 2 shows input fields and automatically shows the keyboard.
  • View 3 shows a results table. In this view one can go one step back (to view 2) or back to the home screen (view 1).
  • 视图 1 显示了一个简单的菜单。
  • 视图 2 显示输入字段并自动显示键盘。
  • 视图 3 显示了一个结果表。在此视图中,您可以后退一步(查看 2)或返回主屏幕(查看 1)。

The keyboard and everything works perfectly the first time, when the user moves from view 1 to view 2. However, when moving either back from view 3 to view 2, or from view 3 and starting all over again, the keyboard does not show.

当用户从视图 1 移动到视图 2 时,键盘和一切在第一次完美运行。但是,当从视图 3 向后移动到视图 2,或从视图 3 重新开始时,键盘不会显示。

In the code, I have becomeFirstResponder in the viewDidAppear method. Also, it will resignFirstResponder when loading view 3. Another interesting fact is that when calling the becomeFirstResponder for the second time, it does return NO (while it returns YES the first time). Also, when returning back to this view the textfields are not possible to select, even though they are userInteractionEnabled.

在代码中,我在viewDidAppear方法中变成了FirstResponder。此外,它会在加载视图 3 时 resignFirstResponder。另一个有趣的事实是,当第二次调用 becomeFirstResponder 时,它确实返回 NO(而第一次返回 YES)。此外,当返回此视图时,无法选择文本字段,即使它们是 userInteractionEnabled。

I am really stuck here, so any help is highly appreciated. I have seen several similar questions to this one, but not found a solution:

我真的被困在这里,所以非常感谢任何帮助。我看过几个与此类似的问题,但没有找到解决方案:

回答by Jarle Hansen

I was able to track down the problem in my code. Answering this question myself since it might be helpful to others as well.

我能够在我的代码中找到问题所在。我自己回答这个问题,因为它可能对其他人也有帮助。

My issue was that I didn't really get the order of events correct. I had a button that would move to the next textfield, and if everything was completed it would move to the next screen. This caused the becomeFirstResponder method to be called and not released (resignFirstResponder) when switching to a new screen.

我的问题是我没有真正得到正确的事件顺序。我有一个按钮可以移动到下一个文本字段,如果一切都完成了它就会移动到下一个屏幕。这导致在切换到新屏幕时调用了 becomeFirstResponder 方法并且没有释放(resignFirstResponder)。

I guess the main advice I can give (at least what finally worked for me), was to just go through all the becomeFirstResponder and make sure it is released before moving to next screen.

我想我可以给出的主要建议(至少最终对我有用的)是通过所有 becomeFirstResponder 并确保它在移动到下一个屏幕之前被释放。