0

I am trying to get 3 different values from 3 different Uitextfields and then save them into 1 string to send it to a rest API server through a button action. I am stucked on how to get all 3 texts inputs from the 3 UItextfields.Any thought? My textfields are account,username,password.

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

Can't find away to get all 3 datas when using the :

 -(void) textFieldDidEndEditing:(UITextField *)textField

function.

Thanks in advance!

4

3 回答 3

1

设置文本字段标签

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

    }
于 2013-01-07T15:09:46.083 回答
0

您应该将 UITextFields 保留为 ivars,并关联 NSStrings(userNameString、passwordString、accountString)。然后在以下方法中,您应该检查哪个 textField 调用了您的委托并将 textField 的文本分配给正确的字符串。您还可以使用 textfield 标记属性来区分它们,然后您不需要将它们保留为 ivars..

   -(void) textFieldDidEndEditing:(UITextField *)textField{
    if (textField==userName)
       userNameString = textField.text;
    if (textField== account)
       accountString = textField.text
    if (textField== password)
       password String = textField.text;
    }
于 2013-01-07T15:01:03.793 回答
0
  1. 给每个文本字段一个tag,例如123
  2. 通过textFieldDidEndEditing:标签区分三个文本字段:

.

if (textField.tag == 1) {
   // assign the text to the right variable
}
... etc.

如果您遵循这种模式,您可以避免使用许多属性使您的类变得混乱。

为了使代码更具可读性,您可以使用以下模式:

#define userNameTextField 1
#define accountTextField 2
#define passwordTextField 3

或者

typedef enum { 
   userNameTextField = 1,
   accountTextField,
   passwordTextField
} TextFields;
于 2013-01-07T15:01:38.180 回答