UITextField 的标签在其键盘设置为数字键盘时重置?

Posted

技术标签:

【中文标题】UITextField 的标签在其键盘设置为数字键盘时重置?【英文标题】:UITextField's tag reset when its keyboard is set to number pad? 【发布时间】:2013-12-20 12:12:43 【问题描述】:

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

我正在给文本字段添加标签,以便在 textFieldShouldReturn 方法中处理下一步/完成按钮。

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

我该如何处理这种情况?

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

【问题讨论】:

您确定没有在代码中的某处使用错误的if 语句重置标签吗?喜欢:if (aView.tag = x) 而不是 if (aView.tag == x)... 请添加您的代码!!! @rokjarc 是的,我确定 【参考方案1】:

看看

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;

【讨论】:

以上是关于UITextField 的标签在其键盘设置为数字键盘时重置?的主要内容,如果未能解决你的问题,请参考以下文章

iOS 键盘处理(改变键盘为完成键),UITextField键盘显示隐藏,弹出,回弹

UITextField - resignFirstResponder 查询

iOS之UITextField怎么自定义键盘的return键

禁用iOS键盘中的输入按钮

IOS 键盘 禁止输入字母

如何将 UITextField 数据复制到 UILabel