0

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

3 Answers 3

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;
Sign up to request clarification or add additional context in comments.

4 Comments

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.
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.
Witch NSMutableArray you talking about ?
Hai Benoit, I got result with your help. Thanks!
1

Assign tag for each UITextField..

then access the textField using

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

3 Comments

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.
include this line txt.tag=i; in ur for loop
then retrieve the value as follows,for(int i=1; i<=5;i++) { UITextField *tf=(UITextField *)[self.view viewWithTag:i]; [mutArr addObject:tf.text];}
0
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];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.