Dimitris Koumouras
Dimitris Koumouras

Reputation: 97

Assign data from 3 UItextfields into 3 different strings

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!

Upvotes: 0

Views: 143

Answers (3)

laxonline
laxonline

Reputation: 2653

Set textfield tag

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

Save the values in 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;
        }

    }

Upvotes: 1

Mundi
Mundi

Reputation: 80265

  1. give each text field a tag, e.g. 1, 2 and 3
  2. in textFieldDidEndEditing: distinguish the three text fields by tag:

.

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

If you follow this pattern, you can avoid cluttering your class with many properties.

To make the code more readable, you can use this pattern:

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

or

typedef enum { 
   userNameTextField = 1,
   accountTextField,
   passwordTextField
} TextFields;

Upvotes: 0

David Ben Ari
David Ben Ari

Reputation: 2299

you should keep the UITextFields as ivars, as well as correlating NSStrings (userNameString, passwordString, accountString). then on the following method you should check which textField invoked your delegate and assign the textField's text to the correct string. you can also use the textfield tag property to differentiate between them, and then you don't need to keep them as ivars..

   -(void) textFieldDidEndEditing:(UITextField *)textField{
    if (textField==userName)
       userNameString = textField.text;
    if (textField== account)
       accountString = textField.text
    if (textField== password)
       password String = textField.text;
    }

Upvotes: 0

Related Questions