0

我有一个带有标签的文本字段。以下循环创建 20 个文本字段。

txt_Pos_Y =20;

for (j=0; j<20; j++) {

if (j<20) {
txtField = [[UITextField alloc] initWithFrame:CGRectMake(110, txt_Pos_Y, 100, 15)];
txtField.borderStyle = UITextBorderStyleRoundedRect;
txtField.font = [UIFont systemFontOfSize:11];
txtField.tag = j;
txtField.placeholder = @"Enter Value";
txtField.autocorrectionType = UITextAutocorrectionTypeNo;
txtField.keyboardType = UIKeyboardTypeDefault;
txtField.returnKeyType = UIReturnKeyDone;
txtField.clearButtonMode = UITextFieldViewModeWhileEditing;
txtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;    
txtField.delegate = self;
[scrollView addSubview:txtField];
[txtField release];  
}
}


-(void)CalculateMethod{

txtField.tag = 1;


double a;

a = Var_sqFt * Var_sqMtr;

txtField.text = [[NSString alloc] initWithFormat:@"%.4f",a];    
}   

在 CalculateMethod() 我想在 txtField.text 中显示“a”的值,但我无法在 txtField.tag = 1 上打印该值;

该值显示在底部 textfeild 表示 txtField.tag =19;

我想在 txtField.tag = 1 文本字段上显示“a”的值。

像这样

txtField.tag = 1 txtField.text.tag = [[NSString alloc] initWithFormat:@"%.4f",a];

这该怎么做?

4

2 回答 2

3

用这个

-(void)CalculateMethod {
    UITextField *txtFld = (UITextField*)[scrollView viewWithTag:1];
    double a;
    a = Var_sqFt * Var_sqMtr;
    txtFld.text = [[NSString alloc] initWithFormat:@"%.4f",a];
}
于 2012-08-13T13:11:56.947 回答
0

版本 #1

NSMutableDictionary *textFieldsDictionary = [NSMutableDictionary dictionary]; // SPOt THe DIFFERENCE HERE

CGFloat txt_Pos_Y =20;

for (int j=0; j<20; j++) {
    UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(110, txt_Pos_Y, 100, 15)];
    txtField.borderStyle = UITextBorderStyleRoundedRect;
    txtField.font = [UIFont systemFontOfSize:11];
    txtField.tag = j;
    txtField.placeholder = @"Enter Value";
    txtField.autocorrectionType = UITextAutocorrectionTypeNo;
    txtField.keyboardType = UIKeyboardTypeDefault;
    txtField.returnKeyType = UIReturnKeyDone;
    txtField.clearButtonMode = UITextFieldViewModeWhileEditing;
    txtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    txtField.delegate = self;
    [scrollView addSubview:txtField];
    [textFieldsDictionary setObject:txtField forKey:[NSString stringWithFormat:@"%d", j]]; // SPOT THE DIFFERENCE HERE
    [txtField release];
}

和:

-(void)CalculateMethod{
    UITextField *txtField = [textFieldsDictionary valueForKey:[NSString stringWithFormat:@"%d", 1]];
    double a = 3.1415f;
    txtField.text = [[NSString alloc] initWithFormat:@"%.4f", a]; 
}

版本 #2

您应该只更改此方法。

-(void)CalculateMethod{
    UITextField *txtField = nil;
    for (id temporarySubView in scrollView.subviews) {
        if ([temporarySubView isKindOfClass:[UITextField class]]) {
            if (((UITextField *)temporarySubView).tag == 1) {
                txtField = temporarySubView;
                break;
            }
        }
    }

   double a = 3.1415f;
   txtField.text = [[NSString alloc] initWithFormat:@"%.4f", a];
}   
于 2012-08-13T15:02:55.207 回答