2012-03-15 4 views
0

タグを使ってテキストフィールドを動的に追加して、毎回一意の値を与えることができます。そしてそれらの値を加えてラベルに表示するよりも。ボタン1をクリックすると、 "n"が値を与え、その値が前の値に加算されます。タグ付きのテキストフィールドを動的に追加オンボタンパート2

値の追加に成功しました。しかし、私は何を編集するとき、変更したり、ループが原因でこのラインの再実行されますように(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; 
[email protected]""; 
//[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]; 
} 

ありがとうございます。

答えて

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 

は、それはすべての値を再計算します。

+0

gr8 help sir、今、私のコードは正常に動作していますが、ありがとう、ありがとう、私は "Txt_New_Estimated"で別の名前のテキストフィールドを5個追加したいので、このコードに5つの新しいテキストフィールドを追加する方法int result = 0; for(textFieldArrayのUITextField *フィールド)//追加されたすべてのUITextFieldを反復します。 { result + = [field.text intValue]; //またはfloatValue } NSLog(@ "結果は%d"、結果);/ –

+0

'UITextField'を作成するたびに配列に追加し、その総和を計算すると' for'ループを繰り返すだけです。変数 'Txt_New_Estimated'以外は何も変わりません。 –

+0

もう一度gr8ヘルプサー私が欲しかったように正しく動作するコード、ありがとうございました。多くの先生..... –

関連する問題