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 ?
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];
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With