Iphone 动态添加按钮第2部分带有标记的文本字段

Iphone 动态添加按钮第2部分带有标记的文本字段,iphone,ios,xcode,uitextfield,Iphone,Ios,Xcode,Uitextfield,我想动态添加一个带有标记的textfield,这样它每次都能给出唯一的值。然后添加这些值并显示在标签上。当我点击按钮时,一个文本字段加上“n”给出值,该值与前一个值相加 增值成功。但当我编辑任何内容时,更改或给出另一个值,例如(10而不是12),循环将再次运行,因为这一行 [Txt_New_Estimated addTarget:self action:@selector(C6Loop) forControlEvents:UIControlEventEditingDidEnd]; 第二个问题是

我想动态添加一个带有标记的textfield,这样它每次都能给出唯一的值。然后添加这些值并显示在标签上。当我点击按钮时,一个文本字段加上“n”给出值,该值与前一个值相加

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

[Txt_New_Estimated addTarget:self action:@selector(C6Loop) forControlEvents:UIControlEventEditingDidEnd];
第二个问题是,当我添加一个新的textfield时,之前的textfield没有修改,也没有添加其余的textfields。。。在添加新的文本字段之前,它可以正常工作,但当编辑任何内容时,循环将再次运行。。。。我想克服这个问题,所以请检查此代码并给出一些可能的解决方案。我正在发送我的代码,请检查此代码

-(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];
}  
-(void)CreateTextFeildOnRun
{
如果(标记==0)
{
textPosY=420;
}

对于(i=tag;i为什么不在创建时将这些
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

无论您是否更改该值,它都将重新计算所有值。

gr8帮助先生,现在我的代码工作正常,非常感谢,但是先生,现在我想在“Txt\U New\U Estimated”中添加5个不同名称的文本字段,那么现在如何在该代码中添加这5个新文本字段int result=0;对于(UITextField*textFieldArray中的字段)//将迭代添加的所有UITextField{result+=[field.text intValue];//或floatValue}NSLog(@“结果为%d”,result)/每当您创建
UITextField
将其添加到数组中,并且在计算它们的和时,只需像我所做的那样对
for
循环进行迭代,就不会有任何更改,除了您的变量
Txt\u New\u Estimated
之外,gr8再次帮助先生,现在我的代码按照我的要求正常工作,非常感谢您…..int result=0;谢谢(UITextField*textFieldArray中的字段)//将迭代添加的所有UITextField{result+=[field.text intValue];//或floatValue}NSLog(@“结果为%d”,result);如何在单个循环中迭代2个不同的数组,例如“”(textFieldArray中的UITextField*字段)//将在单个循环中迭代all和textFieldArray2,以便我可以从两个数组的同一索引中拾取值……您的数组大小是否相同?