becomeFirstResponder not working!
In the below code becomeFirstResonder not working, only resignFirstresponder working...can anyone please help
- (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]|开发者_C百科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;
}
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];
}
}
You don't need the resignFirstResponder calls in there if you're assigning the firstResponder at the same time. That may be confusing things. Also, verify the fields are configured properly; are you able to tap on them to set their firstResponder status?
we must ensure the METHOD becomeFirstResponder be performed on mainThread so make it like :
[xxx performSelectorOnMainThread:@selector(becomeFirstResponder) withObject:nil waitUntilDone:NO];
精彩评论