rnystrom
rnystrom

Reputation: 1916

Is this a hacky way to go to the next UITextField (using textFieldShouldReturn)?

- (BOOL)textFieldShouldReturn:(UITextField *)sender
{
    if(sender.returnKeyType == UIReturnKeyNext){
        // Make something else first responder
    }else if(sender.returnKeyType == UIReturnKeyGo){
        // Do something
    }else{
        [sender resignFirstResponder];
    }
    return YES;
}

I have a UITextFieldDelegate using this. I'm new to iPhone dev stuff. Coming from web, I'm used to defining events dynamically and minimally. Is this an "ok" way to go from Username to Password UITextFields?

Is there a better common practice?

Upvotes: 2

Views: 718

Answers (2)

jrturton
jrturton

Reputation: 119292

This has been discussed extensively here: How to navigate through textfields (Next / Done Buttons)

But the quick answer is yes, this is fine! Nothing hacky about it.

Upvotes: 2

Mark Adams
Mark Adams

Reputation: 30846

If you have the text fields declared as properties of the class, you could simply compare sender to each text field to determine the correct course of action. I would probably do something like this.

- (BOOL)textFieldShouldReturn:(UITextField *)sender
{
    if (sender == self.firstTextField)
        [self.secondTextField becomeFirstResponder];
    else if (sender == self.secondTextField)
        [self doSomething];
    else
        [sender resignFirstResponder];

    return YES;
}

Upvotes: 0

Related Questions