UITextField's tag reset when its keyboard is set to number pad?

291 views Asked by At

I have a number field in which i want my customer to benefit number pad.

I am giving tags to text fields in order to handle next/done button in a textFieldShouldReturn method.

But when text field's keypad is set to number pad, its tag is reset.

How can i handle this situation?

[aliasField setFont:[UIFont fontWithName:@"Helvetica" size:14]];
aliasField.layer.cornerRadius = 4;
aliasField.layer.borderColor = [[UIColor colorWithRed:82.0f/255.0f green:191.0f/255.0f blue:240.0f/255.0f alpha:1.0]CGColor];
aliasField.layer.borderWidth = 1.0f;
[aliasField setTag:1];

[nameLastNameField setFont:[UIFont fontWithName:@"Helvetica" size:14]];
nameLastNameField.layer.cornerRadius = 4;
nameLastNameField.layer.borderColor = [[UIColor colorWithRed:82.0f/255.0f green:191.0f/255.0f blue:240.0f/255.0f alpha:1.0]CGColor];
nameLastNameField.layer.borderWidth = 1.0f;
[nameLastNameField setTag:2];


[cardNumberField setFont:[UIFont fontWithName:@"Helvetica" size:14]];
cardNumberField.layer.cornerRadius = 4;
cardNumberField.layer.borderColor = [[UIColor colorWithRed:82.0f/255.0f green:191.0f/255.0f blue:240.0f/255.0f alpha:1.0]CGColor];
cardNumberField.layer.borderWidth = 1.0f;
[cardNumberField setTag:3];
[cardNumberField setKeyboardType:UIKeyboardTypeNumberPad];

[validDateMonthField setFont:[UIFont fontWithName:@"Helvetica" size:14]];
validDateMonthField.layer.cornerRadius = 4;
validDateMonthField.layer.borderColor = [[UIColor colorWithRed:82.0f/255.0f green:191.0f/255.0f blue:240.0f/255.0f alpha:1.0]CGColor];
validDateMonthField.layer.borderWidth = 1.0f;
[validDateMonthField setTag:4];
[validDateMonthField setKeyboardType:UIKeyboardTypeNumberPad];


[validDateYearField setFont:[UIFont fontWithName:@"Helvetica" size:14]];
validDateYearField.layer.cornerRadius = 4;
validDateYearField.layer.borderColor = [[UIColor colorWithRed:82.0f/255.0f green:191.0f/255.0f blue:240.0f/255.0f alpha:1.0]CGColor];
validDateYearField.layer.borderWidth = 1.0f;
[validDateYearField setTag:5];
[validDateYearField setKeyboardType:UIKeyboardTypeNumberPad];



[cvvField setFont:[UIFont fontWithName:@"Helvetica" size:14]];
cvvField.layer.cornerRadius = 4;
cvvField.layer.borderColor = [[UIColor colorWithRed:82.0f/255.0f green:191.0f/255.0f blue:240.0f/255.0f alpha:1.0]CGColor];
cvvField.layer.borderWidth = 1.0f;
[cvvField setTag:6];
[cvvField setKeyboardType:UIKeyboardTypeNumberPad];

[bankNameField setFont:[UIFont fontWithName:@"Helvetica" size:14]];
bankNameField.layer.cornerRadius = 4;
bankNameField.layer.borderColor = [[UIColor colorWithRed:82.0f/255.0f green:191.0f/255.0f blue:240.0f/255.0f alpha:1.0]CGColor];
bankNameField.layer.borderWidth = 1.0f;
[bankNameField setTag:7];

//// text field should return method

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// The user has pressed the "Return Key"
// Which I have set to "Next" for first two lines
// and "Done" for the last line, so jump to the next text field
NSLog(@"\"Return\" key pressed.");

// based on which text field we are in jump to the next
if (textField.tag == 7)
    // We have reach the last line so hide keyboard
    [textField resignFirstResponder];

// this is where you can perform Core Data updates if you like

else {
    int nextTag = textField.tag + 1;
    UIView *nextField = [self.view viewWithTag:nextTag];
    [nextField becomeFirstResponder];

    // Once the next text field is the first responder
    // I need to make sure the user can see it
    //[self makeActiveTextFieldVisible];
}
return YES;

}

1

There are 1 answers

0
gronzzz On

Check it out

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(15, 0, cell.frame.size.width - 10, cell.frame.size.height)];
            textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
if (something) {
    textfield.tag = 1;
} else {
    textfield.tag = 2;
textField.keyboardType = UIKeyboardTypeNumberPad;
}

textField.returnKeyType =  UIReturnKeyNext; //UIReturnKeyDone;
textField.autocapitalizationType = UITextAutocapitalizationTypeSentences;
textField.adjustsFontSizeToFitWidth = YES;
textField.placeholder = placeholderName;
textField.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:17.0f];
textField.textColor = [UIColor colorWithRed:68.0f/255.0f green:68.0f/255.0f blue:68.0f/255.0f alpha:1.0];
 textField.delegate = self;

UPDATE: in textFieldShouldReturn method simply check tag number.

For example mine method:

-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
    UIResponder* nextResponder;
    switch (textField.tag) {
        case NAME_TAG:
        nextResponder = [_tableView viewWithTag:SUMMARY_TAG];
            break;
        case SUMMARY_TAG:
            nextResponder = [_tableView viewWithTag:NAME_TAG];
            break;

        default:
            break;
    }

    if (nextResponder) {
        // Found next responder, so set it.
        [nextResponder becomeFirstResponder];
    } else {
        // Not found, so remove keyboard.
        [textField resignFirstResponder];
    }

    return NO;
}