2
- (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?

rnystrom
  • 1,906
  • 2
  • 21
  • 47

2 Answers2

2

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.

Community
  • 1
  • 1
jrturton
  • 118,105
  • 32
  • 252
  • 268
0

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;
}
Mark Adams
  • 30,776
  • 11
  • 77
  • 77
  • I like this. It's about the same as mine but gives me more control with my UITextFields. That's one thing I keep forgetting, not putting stuff in IBOutlets and such, instead just creating and adding them on the fly. – rnystrom Sep 18 '11 at 19:50