Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create textfields based on user input

I want to create textfields based on user input. Let's say 5 textfield are needed

for (int x = 0; x < self.howMany; x++) 
{
    NSLog(@"%i", x_position);
    self.textFieldRounded = [[UITextField alloc] initWithFrame:CGRectMake(10, x_position, 300, 25)];
    self.textFieldRounded.borderStyle = UITextBorderStyleRoundedRect;
    self.textFieldRounded.textColor = [UIColor blackColor]; //text color
    self.textFieldRounded.font = [UIFont systemFontOfSize:17.0];  //font size

    [self.view addSubview:self.textFieldRounded];
    x_position += 30;
}

So far, so good. That code create five textfields. The problem is that these textfields are not "unique". Each of these textfield can contain different information. What to do so that I can gather information of each of these fields ?

like image 889
Profo Avatar asked Dec 17 '25 15:12

Profo


2 Answers

Add a tag to them.

int i = 1;
for (int x = 0; x < self.howMany; x++) 
{
    NSLog(@"%i", x_position);
    self.textFieldRounded = [[UITextField alloc] initWithFrame:CGRectMake(10, x_position, 300, 25)];
 textFieldRounded.tag = i++;
 ...

And then, when you need to find textfield 3 for example, do:

UITextField *textField = (UITextField *)[self.view viewWithTag:3];
like image 200
vakio Avatar answered Dec 19 '25 10:12

vakio


You could set a tag for each text field.

self.textFieldRounded.tag = x;

Then you can use the tag to identify your text fields. You can iterate through self.view to find all subviews (text fields) using this technique.

like image 31
pojo Avatar answered Dec 19 '25 09:12

pojo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!