如何从ios中for循环内创建的所有文本字段中获取文本

时间:2016-04-20 04:56:21

标签: ios objective-c for-loop uiview uitextfield

我创建了如下的文本域

for (DataSourceForController *ds in _dataArray)
{

    NSString *name = ds.labelName;
    NSString *bits  = ds.textLabelPlceholder;


    _paramNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width*0, (self.frame.size.height*0.15)+i, self.frame.size.width*0.45,self.frame.size.height*0.05)];
    _paramNameLabel.text = name;
    _paramNameLabel.textColor = [UIColor blueColor];
    _paramNameLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_paramNameLabel];

    _paramValue =  [[UITextField alloc]initWithFrame:CGRectMake(self.frame.size.width*0.45, (self.frame.size.height*0.15)+i, self.frame.size.width*0.55,self.frame.size.height*0.05)];
    _paramValue.placeholder = bits;
    _paramValue.textAlignment= NSTextAlignmentCenter;
    _paramValue.delegate = self;
    _paramValue.backgroundColor = [UIColor colorWithRed:35/255.0 green:185/255.0 blue:235/255.0 alpha:1];
    [self addSubview:_paramValue];
    i=self.frame.size.height*.1+i;
}

如何从每个文本字段中获取文本,如_paramVlue.text

2 个答案:

答案 0 :(得分:2)

您可以在UITextField中添加NSMutableArray。请尝试以下代码:

 NSMutableArray *textFieldArr = [NSMutableArray alloc]]init;
 for (DataSourceForController *ds in _dataArray)
{

NSString *name = ds.labelName;
NSString *bits  = ds.textLabelPlceholder;


_paramNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width*0, (self.frame.size.height*0.15)+i, self.frame.size.width*0.45,self.frame.size.height*0.05)];
_paramNameLabel.text = name;
_paramNameLabel.textColor = [UIColor blueColor];
_paramNameLabel.textAlignment = NSTextAlignmentCenter;
[self addSubview:_paramNameLabel];

_paramValue =  [[UITextField alloc]initWithFrame:CGRectMake(self.frame.size.width*0.45, (self.frame.size.height*0.15)+i, self.frame.size.width*0.55,self.frame.size.height*0.05)];
_paramValue.placeholder = bits;
_paramValue.textAlignment= NSTextAlignmentCenter;
_paramValue.delegate = self;
_paramValue.backgroundColor = [UIColor colorWithRed:35/255.0 green:185/255.0 blue:235/255.0 alpha:1];
[self addSubview:_paramValue];
[textFieldArr addObject:_paramValue];
i=self.frame.size.height*.1+i;
}

现在假设您想在索引2处获取UITextField的文本,然后写入以下行:

 NSString *str = [(UITextField *)[textFieldArr objectAtIndex:2] text];

希望这会对你有所帮助。

答案 1 :(得分:1)

我找到了另一种简单方法

StringBuilder