如何在单个循环中添加多个textField?

时间:2012-07-30 04:49:09

标签: iphone ios textfield

我想在一个循环中添加多个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];

2 个答案:

答案 0 :(得分:5)

请使用此功能,但在.h文件中实现UITextField委托。但您必须根据您的要求更改占位符,例如1表示FirstName,2表示MiddleName等。

首先创建像这样的通用函数,

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

希望这对你有用..

答案 1 :(得分: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;
}];