1

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

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

因此,当我单击电子邮件文本字段键盘上的下一个按钮时,它会将我带到密码文本字段,一旦我完成输入密码,键盘上的发送按钮就会可见。但是当我尝试单击它时,没有执行任何操作

所以,如果我点击发送按钮,它应该会保存我在注册页面中输入的所有数据,并应该将我带到上一个登录页面。像普通的提交按钮一样,我希望键盘中的发送按钮执行操作,而不是其他普通按钮。

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

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

有什么解决办法吗?

4

1 回答 1

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];

}
于 2012-10-24T06:54:39.703 回答