Agent Chocks.
Agent Chocks.

Reputation: 1312

fetching textfield.text values from dynamically generated textfields

I am new to world of Objective C I am generating dynamic textfields and trying to fetch .text value from each textfield on button press but the problem i am facing is only value in last textfield is fetched eg- if 3 dynamic textfeilds are generated and i enter values 1,2,3 in textfeilds respectively i want all these values but currently i am getting value from only last textfeild

the code i am trying is as below //this function generates dynamic textfeilds

      - (void)dynamicTextboxes {
            NSString *string = txt1.text;
           int Pointsvalue = [string intValue];
           int x;     
          for (x=0; x < Pointsvalue ; x++)
            {     
               txtFldFrame1 = [[UITextField alloc] initWithFrame:CGRectMake(100,60*x,120,45)];
               txtFldFrame1.borderStyle=UITextBorderStyleRoundedRect;
               txtFldFrame1.keyboardType=UIKeyboardTypeNumberPad;    

                [self.view addSubview:txtFldFrame1];
               ArrPut=[[NSMutableArray alloc]initWithObjects:txtFldFrame1, nil];      
       }

         }


       // function to Acess the values
           NSString *string = txt1.text;
          int Pointsvalue = [string intValue];
          int x;
          for (x=0; x < Pointsvalue ; x++)
          {
              NSString * values=txtFldFrame1.text;
              [ArrPut addObject:values];
                if(txtFldFrame1.text.length>0)
                {

                 ArrClick=[NSMutableArray array];
               [ArrClick addObjectsFromArray:ArrPut];
             }
            }
               NSLog(@"%@",ArrClick);

can anybody help me out Thanks in advance

Upvotes: 1

Views: 129

Answers (3)

Manu
Manu

Reputation: 4750

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self dynamicTextFields];
}
- (void)dynamicTextFields
{
    NSString *string = @"3";
    int Pointsvalue = [string intValue];
    int x;
    for (x=1; x < Pointsvalue ; x++)
    {
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100,60*x,120,45)];
        textField.borderStyle=UITextBorderStyleRoundedRect;
        [textField setDelegate:self];
        textField.keyboardType=UIKeyboardTypeNumberPad;
        textField.tag = x;
        [self.view addSubview:textField];
    }
}

- (IBAction)btnRetrieveTextFeildValues:(id)sender
{
    NSMutableArray *valuesArray = [[NSMutableArray alloc]init];
    NSString *string = @"3";
    int Pointsvalue = [string intValue];
    int x;
    for (x=1; x < Pointsvalue ; x++)
    {
        UITextField *textField = (UITextField *)[self.view viewWithTag:x];
        NSLog(@"Text - -%@",textField.text);
        if ([textField.text length]!=0)
        {
            [valuesArray addObject:textField.text];
        }
    }
    NSLog(@"ValuesArray -- %@",valuesArray);
}

Upvotes: 1

Manu
Manu

Reputation: 4750

- (void)dynamicTextboxes
{
    NSString *string = txt1.text;
    int Pointsvalue = [string intValue];
    int x;
    for (x=1; x < =Pointsvalue ; x++)
    {
        txtFldFrame1 = [[UITextField alloc] initWithFrame:CGRectMake(100,60*x,120,45)];
        txtFldFrame1.borderStyle=UITextBorderStyleRoundedRect;
        txtFldFrame1.keyboardType=UIKeyboardTypeNumberPad;
        txtFldFrame1.tag = x;
        [self.view addSubview:txtFldFrame1];
        ArrPut=[[NSMutableArray alloc]initWithObjects:txtFldFrame1, nil];
    }

}


// function to Acess the values
UITextField *txtField = (UITextField *)[self.view viewWithTag:x];
NSString *string = txtField.text;
int Pointsvalue = [string intValue];
int x;
for (x=1; x < Pointsvalue ; x++)
{
    if(txtField.text.length>0)
    {   
        NSString * values=txtField.text;
        [ArrPut addObject:values];
    }
}
NSLog(@"%@",ArrClick);

Hey, UIView itself has the tagValue 0... So, loop it from 1... Just check now

Upvotes: 0

Sumit Mundra
Sumit Mundra

Reputation: 3901

NSString *string = txt1.text;
int Pointsvalue = [string intValue];
int x;
  ArrClick=[NSMutableArray array];
for (x=0; x < Pointsvalue ; x++)
{
    NSString * values=txtFldFrame1.text;
    [ArrPut addObject:values];
    UITextField *txtField = (UITextField *)[self.view viewWithTag:x];
    if(txtField.text.length>0)
    {


        [ArrClick addObjectsFromArray:ArrPut];
    }
}
NSLog(@"%@",ArrClick);

Upvotes: 0

Related Questions