保存数据并单击键盘上的“发送”按钮转到上一个视图

时间:2012-10-24 05:50:12

标签: iphone ios5

我有一个注册页面,一旦我在注册时输入我的详细信息,我想点击键盘上的发送按钮(取代返回键) 。 应保存注册页面中的所有数据并带我到上一个登录页面

对于用户名电子邮件文本字段,我使用了下一步按钮,以便切换标签一旦我完成了每个文本字段。它工作正常。

因此,当我点击电子邮件文本字段键盘上的下一个按钮时,它会转到密码文本字段,输入完密码后,键盘上的发送按钮就会显示。但是当我尝试点击它时,不执行任何操作

因此,如果我点击发送按钮,它应该保存我在注册页面中输入的所有数据,并且应该带我到上一个登录页面。表现得像正常提交按钮,我想要键盘上的发送按钮来执行操作而不是其他普通按钮。

这是我在注册页面中发送和下一个键盘按钮的代码,

-(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;
    }

对此有何解决方案?

1 个答案:

答案 0 :(得分:2)

将属性UIReturnType发送到UITextField的UIReturnTypeSend并设置委托

[usernameTextField setReturnType:UIReturnTypeSend];
[usernameTextField setDelegate:self];
[passwordTextField setReturnType:UIReturnTypeSend];
[passwordTextField setDelegate:self];

然后使用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];

}