我有两个UITextfields如下,
如果用户在第一个文本字段中输入4位数,则第二个文本字段应该成为第一个响应者。我试过下面的代码,
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
int num = [textField.text length] - range.length;
if(textField==pin && num ==4)
{
[cpin becomeFirstResponder];
}
}
这就像,用户必须输入第五个数字。我希望第二个文本字段在第一个UITextfield中输入四位数后成为响应者。怎么做?
答案 0 :(得分:4)
您需要省略RANGE参数..
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([textField.text length] == 3 && ![string isEqualToString:@""]) // Verify that replacementString is a digit and not a backspace
{
textField.text = [textField.text stringByAppendingString:string];
[cpin becomeFirstResponder];
return NO;
}
return YES;
}
答案 1 :(得分:1)
未经测试,因为我无法访问IDE atm
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
int num = [textField.text length] + range.length;
if(textField==pin && num ==4)
{
[cpin becomeFirstResponder];
}
}
或者我确定你可以使用int num = string.length
或者将所有内容全部删除,只需使用:if (textField == pin && string.length == 4)
答案 2 :(得分:0)
试试这个:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
int num = [textField.text length];
if(textField==pin && num ==4) {
[cpin becomeFirstResponder];
}
return YES;
}
答案 3 :(得分:0)
请试试这个。这应该工作
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)strin
{
int num = [textField.text length] - range.length;
if(textField== pin && num ==4)
{
[cpin becomeFirstResponder];
return NO;
}
return YES;
}
希望这有帮助。