Sunday, January 17, 2010

Text Fields and dismissing the keyboard

I was going back and making changes to my text fields and keyboard dismissing and wanted to move to the next field only if that field was blank. I relized then, that I had not taken any notes here on how to dismiss a keyboard, or how to assign a new text field. Here is the code to dismiss a field, and then assign the next one. I'll just write some if statements to figure out the order in which I want them to populate if the user needs to enter them all.

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {

if (theTextField == userName) {

[userName resignFirstResponder];

[userPassword becomeFirstResponder];

}

if (theTextField == userPassword) {

[userPassword resignFirstResponder];

[serverAddress becomeFirstResponder];

}

if (theTextField == serverAddress) {

[serverAddress resignFirstResponder];

}

return YES;

}



Ok, I wrote the if statements before I even finished writing this blog. It's messy, so if you know a bette/faster way to do this, do it your way!


- (BOOL)textFieldShouldReturn:(UITextField *)aTextField {

if (aTextField == userName) {

[userName resignFirstResponder];

if ([userPassword.text length] == 0) {

[userPassword becomeFirstResponder];

} else if ([serverAddress.text length] == 0) {

[serverAddress becomeFirstResponder];

}

}

if (aTextField == userPassword) {

[userPassword resignFirstResponder];

if ([serverAddress.text length] == 0) {

[serverAddress becomeFirstResponder];

}

}

if (aTextField == serverAddress) {

[serverAddress resignFirstResponder];

}

return YES;

}

No comments: