当键盘设置为数字键盘时,UITextField的标签重置?

时间:2013-12-20 12:12:43

标签: ios objective-c uitextfield uikeyboard numpad

我有一个数字字段,我希望我的客户受益于数字键盘。

我正在为文本字段添加标签,以便在textFieldShouldReturn方法中处理next / done按钮。

但是当文本字段的键盘设置为数字键盘时,其标签将被重置。

我该如何处理这种情况?

[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];

////文本字段应返回方法

- (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 个答案:

答案 0 :(得分:0)

检查出来

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;

更新: 在textFieldShouldReturn方法中,只需检查标签号。

例如我的方法:

-(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;
}