在创建NSMutableArray时动态计算TextField

时间:2012-03-19 13:41:38

标签: iphone ios xcode

我正在动态创建文本字段并添加和减去它们......

如何在同一个循环中计算并从两个不同数组中选择值?

类似的东西

例如:for(textFieldArray&&&textfieldArray2中的UITextField *字段)

我必须减去该值,我有2个数组,我想从不同的数组中减去两个文本字段的值,但是相同的索引......

我的代码是

int result = 0;
for(UITextField *field in textFieldArray) // will iterate all UITextField which was added
{
result += [field.text intValue]; //or floatValue
}
NSLog(@"the result is %d", result); //here you have sum of all the text fields' text

我想减去这个,但两个文本字段都在不同的数组上,但具有相同的索引......

int y12=([Txt_New_Actual.text intValue]);
float c116= y12-([Txt_New_Estimated.text floatValue]);
Txt_New_ONU.text = [[NSString alloc] initWithFormat:@"%.2f",c116];

1 个答案:

答案 0 :(得分:1)

你可以这样做。取一个变量来共同迭代

UITextField *fieldArray1=nil,*fieldArray2=nil;
for(int i =0;i<textFieldArray1.count;i++)
{
     fieldArray1 = [textFieldArray1 objectAtIndex:i]; //This is field in array1
     fieldArray2 = [textFieldArray2 objectAtIndex:i]; //This is field in array2 and both are at same index.

     //Do your operation here...
}