我试图从3个不同的Uitextfields中获取3个不同的值,然后将它们保存为1个字符串,通过按钮操作将其发送到其他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
功能
提前致谢!
答案 0 :(得分: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;
}
}
答案 1 :(得分: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;
}
答案 2 :(得分:0)
tag
,例如1
,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;