で合計値を示しています。保存し、いくつかの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]];
}
クール、ありがとう。 –