0

3つの異なるUitextfieldから3つの異なる値を取得し、それらを1つの文字列に保存して、ボタンアクションを介してRESTAPIサーバーに送信しようとしています。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

働き。

前もって感謝します!

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をivarとして保持し、NSString(userNameString、passwordString、accountString)を相互に関連付ける必要があります。次に、次のメソッドで、どのtextFieldがデリゲートを呼び出したかを確認し、textFieldのテキストを正しい文字列に割り当てる必要があります。また、textfieldタグプロパティを使用してそれらを区別することもできます。そうすれば、それらをivarとして保持する必要はありません。

   -(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、たとえば12および3
  2. タグで3つのテキストフィールドを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 に答える