成为FirstResponder不尊重键盘设置 [英] becomeFirstResponder Doesn't respect Keyboard settings

查看:44
本文介绍了成为FirstResponder不尊重键盘设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个相当简单的iPhone应用程序来求解二次方程,主要是因为它很容易-至少是概念和数学!

I'm working on a fairly simple iPhone app to solve the quadratic equation, mostly because it's so easy-at least the concepts and math!

我已经在Interface Builder中创建了一个界面,该界面具有几个Labels,3个文本字段(varAfield等)和一个Solve按钮.已经设置了设置为UITextFieldDelegate的3个文本字段,以便它们自动显示数字和标点符号"键盘.该代码用于关闭键盘,然后在用户点击返回键时自动移至下一个变量("C"表示完成",而变量C则为完成"

I've created an interface in Interface Builder that has a couple Labels, 3 text fields (varAfield, etc) and a Solve button. The 3 text fields which are set as UITextFieldDelegate have been set so that they automatically show the "Numbers and Punctuation" keyboard. This code is used to dismiss the keyboard, then automatically move to the next variable when the user taps the return key (which says "Next" except for variable C which says "Done"

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
if (theTextField == varAfield) {
    [varAfield resignFirstResponder];
    [varBfield becomeFirstResponder];
}
if (theTextField == varBfield) {
    [varBfield resignFirstResponder];
    [varCfield becomeFirstResponder];
}
if (theTextField == varCfield) {
    [varCfield  resignFirstResponder];
}
return YES;
}

无论如何,该问题与成为firstFirstResponder的第一个实例有关.键盘应按原样启动,但是使用的是ASCII键盘,而不是数字和标点符号",这是第二次调用它时,它应该可以正常工作.另外,如果我再次从变量A开始,它将正常工作.无论我将第一个实例(首次(也是唯一的)第一次在应用程序中调用)都变成了firstFirstResponder的实例,它的行为都不正确.

Anyway, the problem occurs with the first instance of becomeFirstResponder. The keyboard comes up as it should, however, it's using an ASCII keyboard instead of "Numbers and Punctuation" The second time it's called, it works as it should. Also, if I start from variable A again it will work fine. No matter where I move the first instance of becomeFirstResponder, the first (and only first) time it's called within the app, it doesn't behave correctly.

更新:beforeFirstResponder仍然(即使在第一个实例上)也尊重我对返回键的选择,但是无论设置了哪个键盘,它仍然显示支持ASCII的字符".发生什么了?我已经检查了IB中的所有内容,看起来还可以...

Update: becomeFirstResponder still (even on the first instance) respects my choice of return key, but no matter which keyboard is set, it still shows the "ASCII Capable" one. So what's going on? I've checked everything in IB and it appears to be ok...

推荐答案

我可以确认这仅在设备上发生,而不是在模拟器上发生.我可以在v2.2.1、3.0和3.1中复制这种行为.

I can confirm this happens on the device only, not in the simulator. I can duplicate this behaviour in v2.2.1, 3.0 and 3.1.

如果您有一堆文本字段,并通过如上例中所示在textFieldShouldReturn中调用beginFirstResponder将它们链接在一起,并且所有字段都设置为键盘类型UIKeyboardTypeNumbersAndPunctuation,则对于每个SECOND字段,键盘都将更改为UIKeyboardTypeASCIICapable

If you have a whole pile of text fields, and chain them together by calling becomeFirstResponder in textFieldShouldReturn as shown in the example above, and all the fields are set to keyboard type UIKeyboardTypeNumbersAndPunctuation, for every SECOND field the keyboard will change to UIKeyboardTypeASCIICapable.

我尝试过显式放置setKeyboardType:UIKeyboardTypeNumbersAndPunctuation,但这没什么区别.

I have tried putting setKeyboardType:UIKeyboardTypeNumbersAndPunctuation explicitly but it makes no difference.

有趣的是,只有在您以编程方式调用beginFirstResponder时,它才会发生.如果用户直接在文本字段上单击,键盘将正确显示.

Interesting to note that it only happens if you call becomeFirstResponder programmatically. If the user clicks on the text field directly the keyboard appears correctly.

更有趣的是,如果您在调试器中查看keyboardType属性,即使接口上显示了ASCII键盘,它仍然设置为UIKeyboardTypeNumbersAndPunctuation.

More interestingly, if you look at the keyboardType property in the debugger, it is still set to UIKeyboardTypeNumbersAndPunctuation even though the ASCII keyboard is shown on the interface.

发生这种情况的原因是因为ASCII和数字小键盘是同一视图的不同模式.

The reason it occurs is because the ASCII and numeric keypads are different modes of the same view.

键盘上的"next"/"return"键的默认操作包括从数字视图切换回字母视图,这就是这里发生的情况.

The default action of the "next"/"return" key on the keypad includes swapping from numeric view back to alphabet view and this is what is happening here.

解决方法:从textFieldShouldReturn返回NO,以防止发生默认行为.

The fix: return NO from textFieldShouldReturn to prevent the default behaviour from occurring.

这篇关于成为FirstResponder不尊重键盘设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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