仅使用键盘从多个文本字段中获取字符串

时间:2012-04-17 22:46:42

标签: ios login textfield

我想创建一个包含两个文本字段的登录系统。我希望用户编辑第一个文本字段,然后单击键盘上的下一个跳转到下一个文本字段,编辑后他应该单击go并且系统应该这样做。所以我真正想要的是能够从一个文本字段跳到下一个文本字段,当用户点击go按钮时,最重要的部分从两个字段中获取输入文本(女巫在第二个字段的键盘上)。我的文本字段都标记在IB中。如果不是很清楚,我想实现与Podio的app登录非常相似的东西。这就是我到目前为止所拥有的。任何帮助非常感谢:

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    AppSettings *settings = [AppSettings sharedSettings];
    NSString *password;
    NSString *name;

    if(textField == nameTextField)
    {
        name = textField.text;
        NSLog(@"Name text field: %@",name);
    }
    if(textField == passwordTextField)
    {
        password = textField.text;
        NSLog(@"Password text field: %@",password);        
    }

    //this is where I send a call to the server
    [[SessionInfo sharedInfo] loginWithEmail:settings.userEmailFromLogin 
                                     AndName:name AndPasswordRequest:password];

    [textField resignFirstResponder];
    return YES;
}

2 个答案:

答案 0 :(得分:0)

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    AppSettings *settings = [AppSettings sharedSettings];
    NSString *password;
    NSString *name;

    if(textField == nameTextField)
    {
        name = textField.text;
        [passwordTextField becomeFirstResponder];
        NSLog(@"Name text field: %@",name);
    }
    if(textField == passwordTextField)
    {
        password = textField.text;
        [textField resignFirstResponder];
        NSLog(@"Password text field: %@",password); 
        //this is where I send a call to the server
        [[SessionInfo sharedInfo] loginWithEmail:settings.userEmailFromLogin 
                                         AndName:name AndPasswordRequest:password];  
    }
    return YES;
}

答案 1 :(得分:0)

@yibuyiqu这对我来说真的不起作用。但我解决了如何从多个文本字段中获取数据的问题。只需要将输入中的字符串存储到某些字典对象中,如下所示:

- (void)textFieldDidEndEditing:(UITextField *)textField 
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    AppSettings *settings = [AppSettings sharedSettings];
    NSString *password;
    NSString *name;

    if ([textField.text isEqualToString:@""])
        return;

    switch (textField.tag) {
        case NameFieldTag:
            name = textField.text;
            [userDefaults setObject:name forKey:kloginName];
            settings.userNameFromLogin = [userDefaults objectForKey:kloginName];
            break;

        case PasswordFieldTag:
            password = textField.text;
            [userDefaults setObject:password forKey:klogginPassword];
            settings.userPasswordFromLogin = [userDefaults objectForKey:klogginPassword];
            break;
        default:
            break;
    }
}

在此之后,我使用此处发布的解决方案来解决键盘中的文本字段https://stackoverflow.com/a/1351090/1339876