Reputation: 497
I have a signup page and once I am done with entering my details in signup, I want to click a send button in the keyboard(inplace of return key). It should save all the data in the signup page and take me to the previous login page.
For username and email textfields, i've used the next button, so that it switches the tabs once I am done with each of the textfields. It is working fine.
So, when i click the next button on the email textfield's keyboard, it takes me to the password textfield and once I am done with entering the password, the send button in the keyboard is visible. But when i try to click it, no action is performed.
So, if i click on the send button, it should save all the data that I've entered in the signup page and should take me to the previous login page. Acting like a normal submit button, i want the send button in the keyboard to perform the action rather than the other normal buttons.
This is my code for send and next keyboard buttons in the signup page,
-(IBAction)send
{
NSUserDefaults *stringDefault = [NSUserDefaults standardUserDefaults];
[stringDefault setValue:@"" forKey:@"stringKey"];
[stringDefault setValue:name.text forKey:@"Username"];
[stringDefault setValue:email.text forKey:@"EmailID"];
[stringDefault setValue:password.text forKey:@"password"];
[stringDefault synchronize];
NSLog(@"%@",name.text);
// NSLog(@"%@",email.text);`
// NSLog(@"%@",password.text);
NSString *emailRegEx = @"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx];
//Valid email address
if ([emailTest evaluateWithObject:email.text] == NO)
{
UIAlertView *Alt = [[UIAlertView alloc] initWithTitle:@"Incorrect" message:@"Enter a valid email id" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[Alt show];
[Alt release];
NSLog(@"email not in proper format");
}
NSString *passwordd = @"[A-Za-z]{8,12}";
NSPredicate *passtest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", passwordd];
//Valid email address
if ([passtest evaluateWithObject:password.text] == NO)
{
UIAlertView *Alt = [[UIAlertView alloc] initWithTitle:@"Incorrect" message:@"Enter a valid password" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[Alt show];
[Alt release];
// NSLog(@"email not in proper format");
}
login *l=[[login alloc]initWithNibName:@"login" bundle:nil];
[self presentModalViewController:l animated:YES];
}
-(IBAction)next:(UITextField *)ras{
if (ras == name) {
[ras resignFirstResponder];
[email becomeFirstResponder];
}
else if (ras == email) {
[ras resignFirstResponder];
[password becomeFirstResponder];
}
else if (ras == password) {
[ras resignFirstResponder];
}
//return YES;
}
Any solution for this?
Upvotes: 1
Views: 230
Reputation: 3529
send property UIReturnType to UIReturnTypeSend of UITextField and set delegate
[usernameTextField setReturnType:UIReturnTypeSend];
[usernameTextField setDelegate:self];
[passwordTextField setReturnType:UIReturnTypeSend];
[passwordTextField setDelegate:self];
then use then textFieldDelegateMethod ShouldReturn
-(BOOL)validationSuccess{
NSString *emailRegEx = @"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx];
//Valid email address
if ([emailTest evaluateWithObject:email.text] == NO)
{
UIAlertView *Alt = [[UIAlertView alloc] initWithTitle:@"Incorrect" message:@"Enter a valid email id" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[Alt show];
[Alt release];
NSLog(@"email not in proper format");
return NO;
}
NSString *passwordd = @"[A-Za-z]{8,12}";
NSPredicate *passtest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", passwordd];
//Valid email address
if ([passtest evaluateWithObject:password.text] == NO)
{
UIAlertView *Alt = [[UIAlertView alloc] initWithTitle:@"Incorrect" message:@"Enter a valid password" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[Alt show];
[Alt release];
// NSLog(@"email not in proper format");
return NO;
}
return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
if([self validationSuccess])
{
//Save your data here
[self saveData];
//If you have pushed from loginviewcontroller then you can go back directly
[[self navigationController] popToRootViewControllerAnimated:YES];
//Or if you have presented from loginviewcontroller
[self dismissViewControllerAnimated:YES completion:NULL];
}
return YES;
}
-(void)saveData{
NSUserDefaults *stringDefault = [NSUserDefaults standardUserDefaults];
[stringDefault setValue:@"" forKey:@"stringKey"];
[stringDefault setValue:name.text forKey:@"Username"];
[stringDefault setValue:email.text forKey:@"EmailID"];
[stringDefault setValue:password.text forKey:@"password"];
[stringDefault synchronize];
}
Upvotes: 2