Reputation: 153
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 ?
Upvotes: 3
Views: 413
Reputation: 3177
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];
Upvotes: 4
Reputation: 4164
You could add a reference to each TextField in an NSMutableArray
and then iterate through the array using something like
for (UITextField *tmp in myArray) {
//do stuff with the textfield
}
Upvotes: 0
Reputation: 3020
I think you should take different TextField for each information so that everything should be clear and if you still want to use one text field then you can set the tag for each info.
Upvotes: 0
Reputation: 6042
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.
Upvotes: 1