故障排除成为FirstResponder 不为某些用户显示键盘
我得到了一个 TextView,它应该在按下按钮时显示,并让用户通过键盘输入文本。
- (IBAction) changeName {
changeNameBtn.hidden = YES;
backBtn.hidden = YES;
usernameLabel.hidden = NO;
inputTextField.hidden = NO;
[inputTextField becomeFirstResponder]; // to show keyboard
}
这对我们大多数人来说都很好。
但是对于我的一些测试人员来说,在地理位置较远的位置,键盘无法显示。如何解决这个问题?我们的设备表现不同的原因可能是什么?它对我和其他人来说都很好用。我们都使用相同的设备(iPod、iOS 4.1 - 5.1)。
我尝试的第一件事不起作用:
我猜测在becomeFirstResponder
被触发之前,inputTextField确实以某种方式没有及时变得可见,所以我添加了setNeedsDisplay< /code> 因为我认为这会强制更新。
[inputTextField setNeedsDisplay];
[inputTextField becomeFirstResponder];
我尝试的第二件事不起作用:
if([inputTextField canBecomeFirstResponder]){
[inputTextField becomeFirstResponder];
}
我尝试的第三件事是一种解决方法,要求不幸的用户按下该字段:
- (IBAction) inputTextFieldTouch {
[inputTextField becomeFirstResponder];
}
此解决方法有效,但必须有更好的方法。请告诉我我做错了什么! :) 以及如何解决这样的问题而不能够自己复制它?
I got a TextView
that is supposed to show upon a button press, and let the user enter text through the keyboard.
- (IBAction) changeName {
changeNameBtn.hidden = YES;
backBtn.hidden = YES;
usernameLabel.hidden = NO;
inputTextField.hidden = NO;
[inputTextField becomeFirstResponder]; // to show keyboard
}
This works fine for most of us.
But the Keyboard does not show for some of my testers, at a geographically remote location. How to trouble shoot this? And what could be the reason for the different behavior of our devices? It works fine for me and everybody else. We all use the same devices (iPod, iOS 4.1 - 5.1).
First thing I tried did not work:
I guessed that the inputTextField did in some way not become visible in time, before becomeFirstResponder
was fired, so I added setNeedsDisplay
as I thought that would force an update.
[inputTextField setNeedsDisplay];
[inputTextField becomeFirstResponder];
Second thing I tried did not work:
if([inputTextField canBecomeFirstResponder]){
[inputTextField becomeFirstResponder];
}
Third thing I tried is a workaround, requiring the unlucky user to press the field:
- (IBAction) inputTextFieldTouch {
[inputTextField becomeFirstResponder];
}
This workaround works, but there must be a better way. Please enlighten me about what I am doing wrong! :) And how to approach issues like this without being able to duplicate it myself?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我稍后通过这行代码调用了
becomeFirstResponder
:它对我有用。希望这有帮助。
I called
becomeFirstResponder
later by this line:It worked for me. Hope this help.
当您尝试使其成为第一响应者时,文本视图尚未完成绘制。
您可以尝试对外观进行动画处理并在完成块中设置第一响应者。
The textview has not finished being drawn by the time you try to make it the first responder.
You can try animating the appearance and setting the first responder in the completion block.
从iOS8开始,在UITextView上调用becomeFirstResponder不会在iOS模拟器上显示键盘,但在真实设备上会显示键盘。因此,请务必使用实际设备进行检查。但这在 iOS7 上不会发生。
As of iOS8, calling becomeFirstResponder on a UITextView doesn't show the keyboard for me on the iOS simulator, but it does on the real device. So be sure to check it with an actual device. This doesn't happen on iOS7 though.
如果有人偶然发现这个问题并且您在应用程序中使用了多个
UIWindow
- 请确保您想要的UIWindow
,即您所在的元素启用第一响应者是isKeyWindow == true
。如果没有 - 对其调用makeKeyAndVisible()
。In case anyone stumbles upon this question AND you are using multiple
UIWindow
s in your app - make sure your desiredUIWindow
, where the element you are making first responder on, isisKeyWindow == true
. If not - callmakeKeyAndVisible()
on it.先尝试看看是否可以显示键盘:
Try to see if you can show the keyboard first: