成为第一个响应者无法正常工作!

时间:2010-03-12 20:41:31

标签: iphone uitextfield alert uialertview

在下面的代码中,eventFirstResonder无法正常工作,只有resignFirstresponder工作......有人可以帮忙吗

- (BOOL)textFieldShouldReturn:(UITextField *)textField {


if (textField == txtDate)
{
[txtDate resignFirstResponder];
[txtTime becomeFirstResponder];

}
if (textField == txtTime)
{
[txtTime resignFirstResponder];
[txtAddress becomeFirstResponder];

}
if (textField == txtAddress)
{
[txtAddress resignFirstResponder];
[txtCity becomeFirstResponder];
}
if (textField == txtCity)
{
[txtCity resignFirstResponder];
[txtState becomeFirstResponder];

}
if(textField == txtState)
{

[txtState resignFirstResponder];
[txtZip becomeFirstResponder];

}
if (textField == txtZip)
{
[txtZip resignFirstResponder]; 

}

return NO;
}


- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{

if(textField == txtDate)

{
NSString *dateString = txtDate.text;
NSString *dateRegex = @"^(1[0-2]|0[1-9])/(3[01]|[12][0-9]|0[1-9])/[0-9]{4}$"; 

NSPredicate *dateTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", dateRegex]; 

BOOL validateDate = [dateTest evaluateWithObject:dateString];


if(!validateDate){


UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:nil message:@"Date Error." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert2 show];
[alert2 release];
txtDate.text = nil;

}


}


if(textField == txtTime) 

{


NSString *timeString = txtTime.text;
NSString *timeRegex = @"^(([0]?[0-5][0-9]|[0-9]):([0-5][0-9]))$"; 

NSPredicate *timeTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", timeRegex]; 

BOOL validateTime = [timeTest evaluateWithObject:timeString];

if(!validateTime) {

UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:nil message:@"Incorrect Time Entry." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert2 show];
[alert2 release];

txtTime.text = nil;

}

}



if(textField == txtAddress)

{

NSString *addressString = txtAddress.text;
NSString *addressRegex = @"^[a-z0-9 ]+$"; 

NSPredicate *addressTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", addressRegex]; 

BOOL validateAddress = [addressTest evaluateWithObject:addressString];

if(!validateAddress) {

UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:nil message:@"Incorrect State." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert2 show];
[alert2 release];


txtAddress.text = nil;

}   


}




if(textField == txtState)

{

NSString *stateString = txtState.text;
NSString *stateRegex = @"^(?-i:A[LKSZRAEP]|C[AOT]|D[EC]|F[LM]|G[AU]|HI|I[ADLN]|K[SY]|LA|M[ADEHINOPST]|N[CDEHJMVY]|O[HKR]|P[ARW]|RI|S[CD]|T[NX]|UT|V[AIT]|W[AIVY])$";

NSPredicate *stateTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", stateRegex]; 

BOOL validateState = [stateTest evaluateWithObject:stateString];

if(!validateState) {

UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:nil message:@"Incorrect State." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert2 show];
[alert2 release];

txtState.text = nil;

}   


}



if(textField == txtCity)

{
NSString *cityString = txtCity.text;
NSString *cityRegex = @"^[a-z ]+$"; 

NSPredicate *cityTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", cityRegex]; 

BOOL validateCity = [cityTest evaluateWithObject:cityString];

if(!validateCity) {

UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:nil message:@"Incorrect City." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert2 show];
[alert2 release];

txtCity.text = nil;

}


}



if(textField == txtZip)

{
NSString *zipString = txtZip.text;
NSString *zipRegex = @"^[0-9]{5}([- /]?[0-9]{4})?$"; 

NSPredicate *zipTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", zipRegex]; 

BOOL validateZip = [zipTest evaluateWithObject:zipString];

if(!validateZip) {

UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:nil message:@"Incorrect Zip." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert2 show];
[alert2 release];

txtZip.text = nil;
}


}

return NO;



}

3 个答案:

答案 0 :(得分:1)

You should not do this in textFieldShouldReturn. Try doing this in textFieldDidEndEditing.

In 
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
     [textField resignFirstResponder];
     return YES;
}

In
- (BOOL)textFieldDidEndEditing:(UITextField *)textField
{
     if (textField == txtDate)
{
    [txtTime becomeFirstResponder];

}
if (textField == txtTime)
{

[txtAddress becomeFirstResponder];

}
if (textField == txtAddress)
{
[txtCity becomeFirstResponder];
}
if (textField == txtCity)
{
[txtState becomeFirstResponder];

}
if(textField == txtState)
{
[txtZip becomeFirstResponder];

}
if (textField == txtZip)
{
//[txtZip resignFirstResponder]; 

}

}

答案 1 :(得分:0)

如果您同时分配firstResponder,则不需要resignFirstResponder调用。这可能会令人困惑。另外,验证字段配置正确;你能点击它们来设置他们的第一个响应者状态吗?

答案 2 :(得分:0)

我们必须确保在mainThread上执行METHOD成为FirstResponder 所以就像:

[xxx performSelectorOnMainThread:@selector(becomeFirstResponder) withObject:nil waitUntilDone:NO];