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() で、「a」の値を txtField.text に表示したいのですが、txtField.tag = 1; に値を出力できません。

値は下部に表示されています textfeild は txtField.tag =19; を意味します。

txtField.tag = 1 Textfield に「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 に答える