[objective-c]从动态生成向文本框中提取 textfield.text 值

发布时间: 2016/10/2 15:19:40
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我是新来的世界 Objective C 我正在生成动态 textfields 和试图从每个获取.text 值 textfield 上按下按钮,但我面临的问题是在最后的唯一值 textfield 如回迁-如果 3 动态 textfeilds 生成和我中输入值 1 2、 3 textfeilds 分别我想所有这些值,但目前我将价值得到成唯一最后textfeild

我试的代码是低于 //this 函数生成动态 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);

谁能帮我谢谢你提前吗

解决方法 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);
}
官方微信
官方QQ群
31647020