I try to create the uitextfield dynamically, but i can't create the textfield name.Anybody knows please help me.
3 Answers
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;
4 Comments
Krešimir Prcela
Use this method for text field creation, and use setTag: function for setting unique ID. If you want exact name for each text field, then you can make your own text field class that will be derived from base text field class and it will have the NSString member name.
user523957
Hai Benoit, I used the loop like this for(int i=1; i<=5;i++) { UITextField *txt=[[UITextField alloc]initWithFrame:CGRectMake(50, 30*i, 200, 20)]; txt.backgroundColor=[UIColor greenColor]; [self.view addSubview:txt]; [txt release]; } through this i create the textfield in dynamically and also i can able to enter the data. but i cant able to store that data in the nsmutablearray. because i used uitextfield object name as txt. how can i store all (5 textfield) data. please help me.
Benoît
Witch NSMutableArray you talking about ?
user523957
Hai Benoit, I got result with your help. Thanks!
Assign tag for each UITextField..
then access the textField using
UITextField *tf=(UITextField *)[yourView viewWithTag:tag];
3 Comments
user523957
Hai KingofBliss, I used the loop like this for(int i=1; i<=5;i++) { UITextField *txt=[[UITextField alloc]initWithFrame:CGRectMake(50, 30*i, 200, 20)]; txt.backgroundColor=[UIColor greenColor]; [self.view addSubview:txt]; [txt release]; } through this i create the textfield in dynamically and also i can able to enter the data. but i cant able to store that data in the nsmutablearray. because i used uitextfield object name as txt. how can i store all (5 textfield) data. please help me.
KingofBliss
include this line txt.tag=i; in ur for loop
KingofBliss
then retrieve the value as follows,for(int i=1; i<=5;i++) { UITextField *tf=(UITextField *)[self.view viewWithTag:i]; [mutArr addObject:tf.text];}
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];