tags:

views:

58

answers:

1

I've got two textfields in a row for username and password. When you're finished putting in your username, the most natural thing to do is to just tap on the next textfield, like you would with a web form. But that doesn't work -- you can't edit the next field until you press "Done" on the keyboard for the first field and then tap on the second one.

My question is: is it possible to set up two textfields so that you end editing on the first one and begin editing the second when you tap the second field?

A: 

You will need to do a few things:

  1. Keep track of the active textfield (use textFieldShouldBeginEditing, for example)

  2. Catch touches in your View Controller and if they do not occur in the active textField, cause the active textField to resignFirstResponder and have the new textField becomeFirstResponder.

The following code is untested but should be a good starting point. Note that it will also cause your textField to lose focus if you tap on anything else outside of the active textField.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [[event allTouches] anyObject];
    if ([currentTextView isFirstResponder] && [touch view] != currentTextView) {
        [currentTextView resignFirstResponder];
        if ([[touch view] isKindOfClass:[UITextField class]])
            [[touch view] becomeFirstResponder];
    }
    [super touchesBegan:touches withEvent:event];
}
Tom S
Thanks -- I'll give this a shot.
trevrosen