2013-01-07 89 views
0

我想从3个不同的Uitextfields中获取3个不同的值,然后将它们保存到1个字符串中,并通过按钮操作将其发送到其他API服务器。我很担心如何从3个UItextfield获得所有3个文本输入。任何想法?我的文本字段是帐户,用户名,密码。当使用将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个DATAS:

-(void) textFieldDidEndEditing:(UITextField *)textField 

功能。

在此先感谢!

回答

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

    } 
+0

还有一个小问题。施加的方法或甚至新矿的哪个invovles这个代码后: - (无效)textFieldDidEndEditing:(的UITextField *)的TextField { 开关(textField.tag){ 壳体1: 字符串1 =的TextField.text; 休息;案例2: string2 = textField.text; 休息;案例3: string3 = textField.text; 休息; 默认值: break; } } 我只获取帐户和用户名的值..任何想法? –

+0

从来没有我发现它是一个小错误:)感谢很多人在这你的帮助! –

0

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

-(void) textFieldDidEndEditing:(UITextField *)textField{ 
    if (textField==userName) 
     userNameString = textField.text; 
    if (textField== account) 
     accountString = textField.text 
    if (textField== password) 
     password String = textField.text; 
    } 
0
  1. 给每一文本字段tag,例如123textFieldDidEndEditing:
  2. 按标签区分三种文本字段:

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; 
+0

我如何给文本字段添加标签如果它不在单元格表中? –

+0

设置时:'textField.tag = userNameTextField;'(或'1') – Mundi

+0

最终完成!谢谢你的帮助!!!!! –

相关问题