我试图从3个不同的Uitextfields中获取3个不同的值,然后将它们保存为1个字符串,以通过按钮操作将其发送到rest API服务器。我被困在如何从3个UItextfields中获取所有3个文本输入的地方。我的文本字段是帐户,用户名,密码。

UITextField *account = [[UITextField alloc] initWithFrame:CGRectMake(90, 50, 140, 30)];
account.borderStyle = UITextBorderStyleRoundedRect;
account.font = [UIFont systemFontOfSize:14];
account.placeholder = @"Account";
account.autocorrectionType = UITextAutocorrectionTypeNo;
account.keyboardType = UIKeyboardTypeDefault;
account.returnKeyType = UIReturnKeyDone;
account.clearButtonMode = UITextFieldViewModeWhileEditing;
account.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
account.delegate = self;
[self.view addSubview:account];
[account release];


UITextField *username = [[UITextField alloc] initWithFrame:CGRectMake(90, 80, 140, 30)];
username.borderStyle = UITextBorderStyleRoundedRect;
username.font = [UIFont systemFontOfSize:14];
username.placeholder = @"User ID";
username.autocorrectionType = UITextAutocorrectionTypeNo;
username.keyboardType = UIKeyboardTypeDefault;
username.returnKeyType = UIReturnKeyDone;
username.clearButtonMode = UITextFieldViewModeWhileEditing;
username.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
username.delegate = self;
[self.view addSubview:username];
[username release];

UITextField *password = [[UITextField alloc] initWithFrame:CGRectMake(90, 110, 140, 30)];
password.borderStyle = UITextBorderStyleRoundedRect;
password.font = [UIFont systemFontOfSize:14];
password.placeholder = @"Password";
password.autocorrectionType = UITextAutocorrectionTypeNo;
password.keyboardType = UIKeyboardTypeDefault;
password.secureTextEntry = YES;
password.returnKeyType = UIReturnKeyDone;
password.clearButtonMode = UITextFieldViewModeWhileEditing;
password.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
password.delegate = self;
[self.view addSubview:password];
[password release];


使用时无法找到所有3个数据:

 -(void) textFieldDidEndEditing:(UITextField *)textField


功能。

提前致谢!

最佳答案

设定文字栏位标记

    userNameTextField.tag = 1
    accountTextField.tag = 2
    passwordTextField.tag = 3


将值保存在shouldChangeCharactersInRange

   - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

        switch (textField.tag) {
            case 1:
                //userNameTextField
                NSString *userNameTextField = [NSString stringWithFormat:@"%@%@",textField.text,string];
                //save
                break;
            case 2:
                //accountTextField
                NSString *accountTextField = [NSString stringWithFormat:@"%@%@",textField.text,string];
                //save
                break;
            case 3:
                //passwordTextField
                NSString *passwordTextField = [NSString stringWithFormat:@"%@%@",textField.text,string];
                //save
                break;
            default:
                break;
        }

    }

关于iphone - 将来自3个UItextfields的数据分配给3个不同的字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14198349/

10-13 09:30