Ranjeet Sajwan
Ranjeet Sajwan

Reputation: 1921

Getting the value of textFields using their tag

I have created some textFields dynamically:

    for (int a=0; a<10; a++) {
    UITextField *textField =[[UITextField alloc]initWithFrame:CGRectMake(150, 20+50*a , 160, 31)];
            [textField setBorderStyle:UITextBorderStyleRoundedRect];
            [textField setTag:a+1];
            [textField setDelegate:self];
            [self.view addSubview:textField];
            [textField release];
}

Now I have to get the values of their on a button click using their tag. How can I make this ?

Upvotes: 0

Views: 8586

Answers (2)

Alex Deem
Alex Deem

Reputation: 4805

You can use the viewWithTag: method of UIView to get the subview with the tag you want. From there you can cast it to a UITextField and use the text property to get the value.

Upvotes: 0

Vladimir
Vladimir

Reputation: 170839

You can get your textfields later using -viewWithTag: method:

- (void) buttonClick:(id)sender{
   for (int a=0; a<10; a++) {
       UITextField *textField = (UITextField*)[self.view viewWithTag:a+1];
       NSString *fieldValue = textField.text;
       NSLog(@"Field %d has value: %@", a, fieldValue);
   }
}

Upvotes: 17

Related Questions