1

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

8
  • Got the solution dear ? Commented Apr 6, 2013 at 10:49
  • no sir, trying....... Commented Apr 6, 2013 at 10:50
  • What's your requirement actually ?? Commented Apr 6, 2013 at 10:51
  • i want the values from the generated textfeilds and i have to calculate the interpolation from that values its a mathematical app i am creating. Commented Apr 6, 2013 at 10:55
  • This line: ArrPut=[[NSMutableArray alloc]initWithObjects:txtFldFrame1, nil]; is the cause of your problem. Should be using addObjectAtIndex method of a mutable array. And make sure ArrPut was allocated elsewhere outside the loop. Commented Apr 6, 2013 at 11:00

3 Answers 3

1
- (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);
}
Sign up to request clarification or add additional context in comments.

Comments

0
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);

Comments

0
- (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

4 Comments

nopes this throws exception(unrecognized selector sent to instance 0x918da00')
what ? your code only. I had just get that textField. Okay, will edit
still getting value of last textfeild my input in texfeilds were 2&3 so the output generated was (3,3)
check now, just check your logic too.. I didn't chnage your logic i have just referenced the textField.But, now edited... just check once

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.