1

I'm trying to be clever with my app.

I have 5 UITextField objects stacked on top of each other. I want the user to be able to enter stuff in the first UITextField and then press 'Next' on the keypad and have focus move to the next UITextField.

My strategy for this was to have an array of UITextField objects and when TextFieldOnEditingDidEnd gets called, I would call 'BecomeFirstResponder()' on the text field that appears AFTER the currently selected UITextField.

This strategy works fine on ios 7.x, however, it causes a STACK OVERFLOW in ios 6.

So, I am wondering if the fact that I call 'BecomeFirstResponder()' is forcing TextFieldOnEditDidEnd() to be called on the text field that I just made into the first responder.

Does anybody have any idea if the call to BecomeFirstResponder() forces a call on TextFieldOnEditDidEnd() ? Also, did this behavior change from iOS 6.x to iOS 7.x?

THANKS

Curtis
  • 5,794
  • 8
  • 50
  • 77
  • `TextFieldOnEditDidEnd` was created by you, right? – Gonzo May 25 '14 at 20:08
  • OnEditDidEnd is the event that gets fired when user leaves the text field. I just renamed it. – Curtis May 26 '14 at 00:59
  • 1
    You've definitely created an infinite loop in iOS 6, and, if this doesn't happen in iOS 7, then yes, they changed the underlying architecture somehow. – matt May 26 '14 at 01:56

2 Answers2

1

Not exactly answering your question, but another solution to this would be:

Set tag's on the textFields. Starting form 1, 2, 3... Then, in the code, implement textFieldShouldReturn: like this:

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    UIView * nextView = [textField.superview viewWithTag:textField.tag+1];
    if ([nextView respondsToSelector:@selector(becomeFirstResponder)])
        [nextView becomeFirstResponder];
    return NO;
}
Gonzo
  • 1,533
  • 16
  • 28
1

The solution that will work on both iOS 6 and iOS 7 is to use delayed performance. In your TextFieldOnEditingDidEnd, do not call becomeFirstResponder directly; instead, call it inside a dispatch_after function call with a tiny delay. This will allow the first method to end before the second method starts, thus breaking the infinite loop.

matt
  • 515,959
  • 87
  • 875
  • 1,141