2012-04-19 4 views
1

で合計値を示しています。保存し、いくつかのint型と私がやりたいどのようなラベル

  • ユーザーはUITextFieldで10に入り、UIButtonをタップします。
  • UILabelを示し、UITextFieldは空白になります。
  • ユーザーはUITextFieldに5を入力し、UIButtonをタップします。
  • UILableを示し、UITextFieldが再び空白になります。

次のコードでは、入力した番号がラベルに表示されますが、最初に入力した番号だけでなく、合計を追加して表示するにはどうすればよいですか?

H

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 

@property (nonatomic, strong) IBOutlet UILabel *label; 
@property (nonatomic, strong) IBOutlet UITextField *field; 

@property (nonatomic, strong) NSString *dataFilePath; 
@property (nonatomic, strong) NSString *docsDir; 
@property (nonatomic, strong) NSArray *dirPaths; 

@property (nonatomic, strong) NSFileManager *fileMgr; 
@property (nonatomic, strong) NSMutableArray *array; 

- (IBAction)saveNumber:(id)sender; 

@end 

メートルアレイ上

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 
@synthesize label, field, dataFilePath, docsDir, fileMgr, dirPaths, array; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    fileMgr = [NSFileManager defaultManager]; 
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    docsDir = [dirPaths objectAtIndex:0]; 
    dataFilePath = [[NSString alloc]initWithString:[docsDir stringByAppendingPathComponent:@"data.archive"]]; 

    if ([fileMgr fileExistsAtPath:dataFilePath]) 
    { 
     array = [NSKeyedUnarchiver unarchiveObjectWithFile:dataFilePath]; 
     self.label.text = [array objectAtIndex:0]; 
    } 
    else 
    { 
     array = [[NSMutableArray alloc] init]; 
    } 

} 


- (IBAction)saveNumber:(id)sender 
{ 
    [array addObject:self.field.text]; 
    [NSKeyedArchiver archiveRootObject:array toFile:dataFilePath]; 
    [field setText:@""]; 
    [label setText:[array objectAtIndex:0]]; 
} 

答えて

0

すべての値を繰り返して、合計値に加算する必要があります。これを見てください: -

- (IBAction)saveNumber:(id)sender 
{ 
    [array addObject:self.field.text]; 
    [NSKeyedArchiver archiveRootObject:array toFile:dataFilePath]; 
    [field setText:@""]; 

    // Create an enumerator from the array to easily iterate 
    NSEnumerator *e = [array objectEnumerator]; 

    // Create a running total and temp string 
    int total = 0; 
    NSString stringNumber; 

    // Enumerate through all elements of the array 
    while (stringNumber = [e nextObject]) { 
     // Add current number to the running total 
     total += [stringNumber intValue]; 
    } 

    // Now set the label to the total of all numbers 
    [label setText:[NSString stringWithFormat:@"%d",total]; 
} 

私は可読性のコードをコメントしました。

+0

クール、ありがとう。 –

0

反復、[string intValue]を用いて数値としてすべての値を取得し、別の変数でそれらをまとめます。次に、計算された値を[NSString stringWithFormat: @"%d"]のような書式設定された文字列を使用してラベルに追加します。

+0

私はこれを初めて行います。助けてください、これは私が得たものです: –

+0

for(int i = 0; i <[配列数]; i ++) \t { \t \t NSString * myString = [array objectAtIndex:i]; \t \t double sum = [myString intValue]; \t \t [ラベルセットテキスト:[NSString stringWithFormat:@ "%d"、sum]]; \t} –

+1

これはほぼそうです。ループの前にsumを宣言するだけです。ループの内部で 'sum + = [myString intValue]'を使用します。ループ後にsetTextを移動することができます。 (本当に、thats it。それはコードよりもコードが少ない、liamsの例よりも)あなたはカスタム列挙子を必要としません。 – calimarkus

関連する問題