becomeFirstResponder not working!!!
Posted
by vikinara
on Stack Overflow
See other posts from Stack Overflow
or by vikinara
Published on 2010-03-12T20:41:30Z
Indexed on
2010/03/12
20:47 UTC
Read the original article
Hit count: 952
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]|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;
}
© Stack Overflow or respective owner