how to create uitextfield dynamically using for lo

2020-05-09 09:11发布

问题:

I try to create the uitextfield dynamically, but i can't create the textfield name.Anybody knows please help me.

回答1:

Assign tag for each UITextField..

then access the textField using

UITextField *tf=(UITextField *)[yourView viewWithTag:tag];


回答2:

from How do I create a text field?

To create a text field, use the UITextField class, as shown in Listing 11.

Listing 11: Creating a text field

CGRect textFieldFrame = CGRectMake(0.0, 0.0, 100.0, 30.0);
UITextField *textField = [[UITextField alloc] initWithFrame:textFieldFrame];
[textField setBorderStyle:UITextFieldBorderStyleBezel];
[textField setTag:1234];
[textField setTextColor:[UIColor blackColor]];
[textField setFont:[UIFont systemFontOfSize:20]];
[textField setDelegate:self];
[textField setPlaceholder:@"<enter text>"];
[textField setBackgroundColor:[UIColor whiteColor]];
textField.keyboardType = UIKeyboardTypeDefault;


回答3:

for (int i=0; i<[checklistArray count]; i++) {

[self addUITextFieldMethod:i]

}

-(void) addUITextFieldMethod:(int)y {

   UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(20, 70*y+40, 280, 31)];
    textField.placeholder  = @"Click here to type";
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.font = [UIFont systemFontOfSize:15];
    textField.textAlignment = NSTextAlignmentLeft;
    textField.returnKeyType = UIReturnKeyDefault;
    int DropDownTag = [[NSString stringWithFormat:@"10%d",y]integerValue];
    textField.tag = DropDownTag ;
    textField.delegate = self;
    [textField addTarget:self action:@selector(returnFromKeyboard:) forControlEvents:UIControlEventEditingDidEndOnExit];

    [scrollViewChecklist addSubview:textField];

}
int textFieldTagValue = textField.tag;

/****************Code In Your Method****************/

UITextField *myTextField = (UITextField *)[self.view viewWithTag:textFieldTagValue];
        myTextField.text = [arrayTest objectAtIndex:indexPath.row];