使用FirstResponder与iPhone SDK的文本验证问题
问题描述:
我想验证我的文本字段,以便如果有问题,用户输入它会捕获它并显示警报(得到该部分),而不是去下一个领域(不能得到那部分)。任何想法,我在做什么错了?使用FirstResponder与iPhone SDK的文本验证问题
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == txtUserName)
{
[txtUserName2 becomeFirstResponder];
}
else if (textField == txtUserName2)
{
[txtUserName3 becomeFirstResponder];
}
else if (textField == txtUserName3)
{
[txtUserName4 becomeFirstResponder];
}
else if (textField == txtUserName4)
{
[txtUserName5 becomeFirstResponder];
}
return NO;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if (textField == txtUserName)
{
NSString *userNameOne = txtUserName.text;
double numOne = [userNameOne intValue];
if(numOne < 40 || numOne > 100)
{
//play sound and vibrate for alert
NSString *bonkSoundFile = [[NSBundle mainBundle] pathForResource:@"alertSound" ofType:@"mp3"];
NSURL *fileURL = [NSURL fileURLWithPath:bonkSoundFile];
SystemSoundID bonkSoundID;
AudioServicesCreateSystemSoundID((CFURLRef) fileURL, &bonkSoundID);
AudioServicesPlaySystemSound(bonkSoundID);
AudioServicesPlaySystemSound (kSystemSoundID_Vibrate); //vibrate
//show alert
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Age Error"
message:@"Your age must be at least 40 years old and less than 100 years old"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
//if there is an error, then don't go to next field but make current textField the //FirstResponder
[txtUserName becomeFirstResponder];
}
}
等共五个字段。我只是不知道我做错了什么......
答
您是否尝试过把验证代码为
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
,并让它不归路,当你不希望继续到下一个领域?
啊你完全正确!我会试试看看它是否有效:) – HollerTrain 2010-01-13 01:15:21