views:

1145

answers:

3

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!

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;
}

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.

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...

A: 

Have you tried just calling becomeFirstResponder on some other view first, so that when you call it on your text field it's not the first time?

By the way, the first two resignFirstResponders are redundant. They might even be what's causing the bug to be revealed.

hatfinch
hmm, I wasn't sure if it was redundant or not. Anyway, removing them causes no change on the behavior of the app.
+4  A: 

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.

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.

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

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.

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.

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

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.

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

Crufty
A: 

Thanks so much Crufty. Returning NO solved this problem for me!

fn686