我正在使用两种形式的文本验证(一种是当他们点击下一步,另一种是当他们手动选择下一个要输入的文本字段时),并且它一直在完美。但是,最后一个文本字段没有得到错误检查,我无法弄明白。
您可以在最后一个文字字段(http://screencast.com/t/ODJiOTAwMzA)中查看此视频中的问题。你可以看到前四个工作正常,但底部没那么多。
这是我的代码:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == txtUserName)
{
NSString *userNameOne = txtUserName.text;
double numOne = [userNameOne doubleValue];
if(numOne < 30 || numOne > 80)
{
//foo
[txtUserName becomeFirstResponder];
txtUserName.text = nil;
}
else
{
[txtUserName2 becomeFirstResponder];
}
}
else if (textField == txtUserName2)
{
NSString *userNameThree = txtUserName2.text;
float numTwo = [userNameThree doubleValue];
if (numTwo < 20 || numTwo > 32)
{
//foo
[txtUserName2 becomeFirstResponder];
txtUserName2.text = nil;
}
else
{
[txtUserName3 becomeFirstResponder];
}
}
else if (textField == txtUserName3)
{
NSString *userNameThree = txtUserName3.text;
float numThree = [userNameThree doubleValue];
if (numThree < 475 || numThree > 650)
{
//foo
[txtUserName3 becomeFirstResponder];
txtUserName3.text = nil;
}
else
{
[txtUserName4 becomeFirstResponder];
}
}
else if (textField == txtUserName4)
{
NSString *userNameFour = txtUserName4.text;
double numFour = [userNameFour doubleValue];
if (numFour < 0.5 || numFour > 3.00)
{
//foo
[txtUserName4 becomeFirstResponder];
txtUserName4.text = nil;
}
else
{
[txtUserName5 becomeFirstResponder];
}
}
else if (textField == txtUserName5)
{
NSString *userNameFive = txtUserName5.text;
double numFive = [userNameFive doubleValue];
if (numFive > 1)
{
//foo
}
}
return NO;
}
在这里
if (textField == txtUserName)
{
NSString *userNameOne = txtUserName.text;
double numOne = [userNameOne doubleValue];
if(numOne < 30 || numOne > 80)
{
//foo
[txtUserName becomeFirstResponder];
txtUserName.text = nil;
}
else
{
[txtUserName2 becomeFirstResponder];
}
}
else if (textField == txtUserName2)
{
NSString *userNameThree = txtUserName2.text;
float numTwo = [userNameThree doubleValue];
if (numTwo < 20 || numTwo > 32)
{
//foo
[txtUserName2 becomeFirstResponder];
txtUserName2.text = nil;
}
else
{
[txtUserName3 becomeFirstResponder];
}
}
else if (textField == txtUserName3)
{
NSString *userNameThree = txtUserName3.text;
float numThree = [userNameThree doubleValue];
if (numThree < 475 || numThree > 650)
{
//fo
[txtUserName3 becomeFirstResponder];
txtUserName3.text = nil;
}
else
{
[txtUserName4 becomeFirstResponder];
}
}
else if (textField == txtUserName4)
{
NSString *userNameFour = txtUserName4.text;
double numFour = [userNameFour doubleValue];
if (numFour < 0.5 || numFour > 3.00)
{
//foo
[txtUserName4 becomeFirstResponder];
txtUserName4.text = nil;
}
else
{
[txtUserName5 becomeFirstResponder];
}
}
else if (textField == txtUserName5)
{
NSString *userNameFive = txtUserName5.text;
double numFive = [userNameFive doubleValue];
if (numFive > 1)
{
//foo
}
else
{
[txtUserName5 becomeFirstResponder];
}
}
答案 0 :(得分:0)
如果您的界面在界面生成器中,是否连接了txtUserName5的出口?
对于你的第五个文本字段,为什么你不像在其他文本字段中那样在“foo”后面有这些行?
if (numFive > 1)
{
//foo
[txtUserName5 becomeFirstResponder];
txtUserName5.text = nil;
}
另外,对于你的else子句,你应该再次成为第一响应者,而不是再次成为第一响应者(以解雇键盘):
else
{
//[txtUserName5 becomeFirstResponder];
[txtUserName5 resignFirstResponder];
return YES;
}