从动态生成的文本字段中获取textfield.text值

时间:2013-04-06 09:46:57

标签: iphone ios objective-c ios5

我是Objective C世界的新手 我正在生成动态textfields,并尝试在按下按钮时从每个textfield获取.text值,但我面临的问题是仅获取最后textfield中的值 例如 - 如果生成3个动态textfeilds并且我分别在textfeilds中输入值1,2,3我想要所有这些值,但目前我只从最后textfeild获得价值

我正在尝试的代码如下 //此函数生成动态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);

任何人都可以帮助我 提前致谢

3 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分: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);

嘿,UIView本身有tagValue 0 ......所以,从1开始循环... 现在就检查

答案 2 :(得分: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);