0

我想在一个循环中添加多个 UITextField。并在用户与 UITextFields 交互时识别它们。所以请帮助我。

谢谢

目前我将 UITextField 单独放置,像这样

txt_FirstName=[[UITextField alloc]initWithFrame:CGRectMake(framex1, framey1, framex2, framey2)];
txt_FirstName.borderStyle = UITextBorderStyleRoundedRect;
txt_FirstName.font = [UIFont systemFontOfSize:15];
txt_FirstName.placeholder = @"First Name";
txt_FirstName.autocorrectionType = UITextAutocorrectionTypeNo;
txt_FirstName.keyboardType = UIKeyboardTypeDefault;
txt_FirstName.returnKeyType = UIReturnKeyDone;
txt_FirstName.clearButtonMode = UITextFieldViewModeWhileEditing;
txt_FirstName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;    
txt_FirstName.delegate = self;
[frameView addSubview:txt_FirstName];
[txt_FirstName release];

framey1=framey1+50;

txt_MiddleName=[[UITextField alloc]initWithFrame:CGRectMake(framex1, framey1, framex2, framey2)];
txt_MiddleName.borderStyle = UITextBorderStyleRoundedRect;
txt_MiddleName.font = [UIFont systemFontOfSize:15];
txt_MiddleName.placeholder = @"Middle Name";
txt_MiddleName.autocorrectionType = UITextAutocorrectionTypeNo;
txt_MiddleName.keyboardType = UIKeyboardTypeDefault;
txt_MiddleName.returnKeyType = UIReturnKeyDone;
txt_MiddleName.clearButtonMode = UITextFieldViewModeWhileEditing;
txt_MiddleName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;    
txt_MiddleName.delegate = self;
[frameView addSubview:txt_MiddleName];
[txt_MiddleName release];
4

2 回答 2

5

请使用它,但在您的 .h 文件中实现 UITextField 委托。但您应该根据您的要求更改占位符,例如 1 代表名字,2 代表中间名等。

首先创建这样的通用功能,

-(void)setTextfieldStyle:(UITextField *)pTmpTextField
{
        pTmpTextField.borderStyle = UITextBorderStyleRoundedRect;
        pTmpTextField.font = [UIFont systemFontOfSize:15];
        pTmpTextField.placeholder = @"First Name";
        pTmpTextField.autocorrectionType = UITextAutocorrectionTypeNo;
        pTmpTextField.keyboardType = UIKeyboardTypeDefault;
        pTmpTextField.returnKeyType = UIReturnKeyDone;
        pTmpTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        pTmpTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;    
        pTmpTextField.delegate = self;
}

并使用它来创建 UITextFiled,

int y = 0;
for(int i=0;i < 5;i++)
{
    UITextField *txtTemp=[[UITextField alloc]initWithFrame:CGRectMake(0, y, 300, 31)];
    txtTemp.tag = i;
    [self setTextfieldStyle:txtTemp];
    [self.view addSubview:txtTemp];
    [txtTemp release];
    y+=36;
}

这是 UITextField 的委托方法,

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSLog(@"%@",textField.text);
}

希望这对你有用..

于 2012-07-30T04:53:46.810 回答
0

如果您想在循环中设置一堆文本字段的属性,您还可以执行以下操作:

NSMutableArray* allTextFields = [NSMutableArray array];

// create the textFields here, I'm assuming you need to keep individual
// references to them.
txt_FirstName=[[UITextField alloc]init];
pTmpTextField.placeholder = @"First Name";
[allTextFields addObject:txt_FirstName];

// now add the common style stuff and frame
[allTextFields enumerateObjectsUsingBlock:^(UITextField* textField, NSUInteger idx, BOOL* stop) {
    textField.frame = CGRectMake(framex1, framey1 + ( 50 * idx), framex2, framey2);
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.font = [UIFont systemFontOfSize:15];
    textField.placeholder = @"First Name";
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    textField.keyboardType = UIKeyboardTypeDefault;
    textField.returnKeyType = UIReturnKeyDone;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;    
    textField.delegate = self;
}];
于 2012-07-30T06:32:15.563 回答