3

This might be a simple question to someone.

How do I control the return key for the keyboard. I have one setup like this:

usernameField.returnKeyType = UIReturnKeyNext;

The other like this:

passwordField.returnKeyType = UIReturnKeyDone;

I would like different methods for each, but that could also be controlled by an if statement on the textField.tag if necessary.

Secondly, is there anyway to control (have some code run) when the close button within a UITextField is clicked. I am referring to the light x shown in the textfield once there is text within the field, that clears the field of its data. Or a way to not have this close icon within the field?

StuartM
  • 6,743
  • 18
  • 84
  • 160

2 Answers2

4

You can check for the textfield and then call the corresponding method required as,

- (BOOL)textFieldShouldReturn:(UITextField *)textField //or implement - (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField == self.usernameField) {
       [self callUserNameMethod];
    } else if (textField == self.passwordField) {
       [self callPasswordMethod];
    }

    [textField resignFirstResponder];
    return YES;
}

Tag property can also be used as mentioned in your question. Note that there are no other way to set a method on tap of default return button in keypad other than using these delegates. There are notifications available for keyboard show and hide which can be used if required.

You can implement the delegate method for clear button as:

- (BOOL)textFieldShouldClear:(UITextField *)textField {
     //write code to be executed on tap of clear button
     return YES;
}

Or you can set the clearButtonMode to be UITextFieldViewModeNever to disable it.

iDev
  • 23,310
  • 7
  • 60
  • 85
1

To answer the second part of your question,

usernameField.clearButtonMode = UITextFieldViewModeNever;
bdesham
  • 15,430
  • 13
  • 79
  • 123