动态添加带有标签的文本字段按钮部分-2

时间:2012-03-15 07:31:56

标签: iphone ios xcode uitextfield

我想用标签动态添加文本字段,以便每次都能给出唯一值。然后添加这些值并在标签上显示。当我单击按钮时,一个文本字段添加“n”给出值,该值将添加到之前的值。

成功增值。但是当我编辑任何内容时,更改或给出另一个值,例如(10而不是12),循环将再次运行,因为这行

[Txt_New_Estimated addTarget:self action:@selector(C6Loop) forControlEvents:UIControlEventEditingDidEnd];

第二个问题是,当我添加一个新的文本字段时,之前的文本字段没有修改,也没有添加其余的文本字段...在添加新的文本字段之前它正常工作但是当编辑任何循环时将再次运行...我想过来解决这个问题,所以请检查一下这个代码并给出一些可能的解决方案。我在这里发送我的代码请检查此代码。

-(void)CreateTextFeildOnRun
{
if (tag ==0)
{
textPosY = 420;
}
for ( i =tag; i<= tag; i++) 
{
Txt_New_Estimated = [[UITextField alloc]initWithFrame:CGRectMake(360, textPosY , 130,         65)];
Txt_New_Estimated.delegate = self;
Txt_New_Estimated.text=@"";
//[Txt_New_Estimated setTag:1234];
Txt_New_Estimated.tag = i; 
Txt_New_Estimated.clearButtonMode = UITextFieldViewModeWhileEditing;
[Txt_New_Estimated addTarget:self action:@selector(C6Loop)    forControlEvents:UIControlEventEditingDidEnd];
Txt_New_Estimated.placeholder = @"Estimated";
Txt_New_Estimated.font = [UIFont fontWithName:@"Arial" size:23];
Txt_New_Estimated.backgroundColor = [UIColor whiteColor];
Txt_New_Estimated.textAlignment = UITextAlignmentCenter;
[scrollview addSubview:Txt_New_Estimated];

}
}

-(void)C6Loop{
Txt_New_ONU.text=Txt_New_Estimated.text;
[self Calculate2];

}

-(void)Calculate2{
int y14=([Txt_New_Estimated.text intValue]);
y14=n;
n=d;

c14=  y14+([Txt_New_Estimated.text floatValue]);

n  = c14;
[self addest];
}


-(void)addest{

float c1= ([Txt_Engring_Est.text floatValue]) + ([Txt_Weddring_Est.text floatValue]) +      ([Txt_Bridal_Est.text floatValue])+ ([Txt_Veil_Est.text floatValue])+ ([Txt_Shoe_Est.text   floatValue])+n;
Txt_Total_Est.text = [[NSString alloc] initWithFormat:@"%.2f",c1];
}  

谢谢。

1 个答案:

答案 0 :(得分:0)

为什么不在创建TextField时将NSMutableArray添加到-(void)createTextFieldOnRun { if (tag ==0) { textPosY = 420; } for ( i =tag; i<= tag; i++) { UITextField *Txt_New_Estimated = [[UITextField alloc]initWithFrame:CGRectMake(360,textPosY ,130,65)]; Txt_New_Estimated.delegate = self; //....other code.. [scrollview addSubview:Txt_New_Estimated]; [textFieldArray addObject:Txt_New_Estimated]; } } 。像这样的东西:

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

当计算时做这样的事情:

{{1}}

无论是否更改值都无关紧要,它将重新计算所有值。