如何在Uitextfield上打印带有标签的值?

时间:2022-01-21 20:29:57

I have a textfield with tag.Following loop creates 20 textfield.

我有一个带有标签的文本字段。以下循环创建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];    
}   

in CalculateMethod() I want to show the value of "a" in txtField.text , but i am not able to print the value on txtField.tag = 1;

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

The value is showing at the bottom textfeild means txtField.tag =19;

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

i want to show the value of "a" on the txtField.tag = 1 Textfield.

我想在txtField.tag = 1 Textfield上显示“a”的值。

like this

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

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

how to do this?

这该怎么做?

2 个解决方案

#1


3  

Use this

-(void)CalculateMethod {
    UITextField *txtFld = (UITextField*)[scrollView viewWithTag:1];
    double a;
    a = Var_sqFt * Var_sqMtr;
    txtFld.text = [[NSString alloc] initWithFormat:@"%.4f",a];
}

#2


0  

VERSION #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];
}

and:

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

VERSION #2

you should change this method only.

你应该只改变这个方法。

-(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];
}   

#1


3  

Use this

-(void)CalculateMethod {
    UITextField *txtFld = (UITextField*)[scrollView viewWithTag:1];
    double a;
    a = Var_sqFt * Var_sqMtr;
    txtFld.text = [[NSString alloc] initWithFormat:@"%.4f",a];
}

#2


0  

VERSION #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];
}

and:

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

VERSION #2

you should change this method only.

你应该只改变这个方法。

-(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];
}