1

如何在循环中计算文本字段?我在循环中有一个文本字段,我想计算另一个文本字段中的输入。

x = 36;     y = 0;   w = 36      h = 25 ;

moretext = 0 ;

for (moretext=0; moretext<5; moretext ++) {
    textFiled1 = [[UITextField alloc]initWithFrame:CGRectMake(x, y, w, h)];
    textFiled1.textAlignment = NSTextAlignmentCenter;
    textFiled1.backgroundColor = [UIColor clearColor];
    textFiled1.font = [UIFont fontWithName:@"Helvetica " size:(8)];
    x+=36 ;

    [self.view addSubview:textFiled1];
}  

我想在 textfield2 中显示 textfield1 循环输入的 TOTAL

textFiled2 = [[UITextField alloc]initWithFrame:CGRectMake(180, 0, 36, 25)];
textFiled2.textAlignment = NSTextAlignmentCenter;
textFiled2.backgroundColor = [UIColor clearColor];
textFiled2.font = [UIFont fontWithName:@"Helvetica " size:(8)];

[self.view addSubview:textFiled2];  
4

2 回答 2

1

实施该UITextFieldDelegate协议,您可以从 UITextField 获取更新作为人员类型。textFieldDidEndEditing:当有人完成编辑文本字段时也会告诉你。

请参阅:http: //developer.apple.com/library/ios/#documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html

于 2013-03-07T17:36:20.677 回答
1

可能您需要修改代码:
在分配多个 UITextField 对象时,您需要将它们存储在这样的某个数组中。

myArray  = [[NSMutableArray alloc]init];
UITextField *textFiled1;

int moretext = 0 ;

for (moretext=0; moretext<5; moretext ++, y=y+50) {
    textFiled1 = [[UITextField alloc]initWithFrame:CGRectMake(x, y, w, h)];
    textFiled1.textAlignment = NSTextAlignmentCenter;
    textFiled1.backgroundColor = [UIColor blueColor];
    textFiled1.font = [UIFont fontWithName:@"Helvetica " size:(8)];
    textFiled1.text = [NSString stringWithFormat:@"%d",y];
    //x+=36 ;
    [myArray addObject:textFiled1];
    [self.view addSubview:textFiled1];
    NSLog(@"view Did Load called");
}

稍后要进行总计,您需要遍历数组并提取文本字段值并累积在像这样的一些变量中。

- (int) calc {
int total=0;
int counter;
for (counter=0; counter<5; counter ++) {
    UITextField *field1 = [myArray objectAtIndex:counter];
    total = total + [field1.text intValue];
    NSLog(@"value: %d",[field1.text intValue]);

}

return total;
}
于 2013-03-09T19:34:24.840 回答