0

このアプリに問題があります。Datapickerの選択を保存する必要があります。

2つのテキストフィールド1つのラベルと1つのデータピッカーがあります。テキストフィールドの「int」は問題ありません。Datapickerの選択を保存するためのコードは何ですか?

#import "ViewController.h"
#import "ResultViewController.h"



 @interface ViewController ()

  @end

@implementation ViewController

@synthesize myLabel;

-(IBAction)calculate:(id)sender{



NSDate *past = _data.date ;
NSDate *now = [NSDate date];

NSCalendar *gregorianCalendar = [[NSCalendar alloc]
                                 initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger unitFlags = NSDayCalendarUnit;
NSDateComponents *components = [gregorianCalendar components:unitFlags
                                                    fromDate:past
                                                      toDate:now
                                                    options:0];
int z = [components day];
int a = ([_textField1.text intValue]);
int b = a*([_textField2.text intValue]);

int r = b * z / 20;

myLabel.text = [[NSString alloc] initWithFormat:@"%d", r];



}

- (void)viewDidLoad
{
[super viewDidLoad];



NSDate *past = _data.date ;
NSDate *now = [NSDate date];

NSCalendar *gregorianCalendar = [[NSCalendar alloc]
                                 initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger unitFlags = NSDayCalendarUnit;
NSDateComponents *components = [gregorianCalendar components:unitFlags
                                                    fromDate:past
                                                      toDate:now
                                                     options:0];
int z = [components day];
int a = ([_textField1.text intValue]);
int b = a*([_textField2.text intValue]);

int r = b * z / 20;

myLabel.text = [[NSString alloc] initWithFormat:@"%d", r];

self.textField1.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"key1"];
self.textField2.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"key2"];




}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {


if ([segue.identifier isEqualToString:@"detail"]) {

    ResultViewController* destVC = (ResultViewController*)segue.destinationViewController;

    destVC.myString = self.myLabel.text;

}
}


- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:YES];



[[NSUserDefaults standardUserDefaults] setObject:self.textField1.text forKey:@"key1"];
[[NSUserDefaults standardUserDefaults] setObject:self.textField2.text forKey:@"key2"];

[[NSUserDefaults standardUserDefaults]synchronize];

}




@end
4

1 に答える 1

0

NSDateはプロパティリストオブジェクトでもあるため、同じ方法で保存できます。

[[NSUserDefaults standardUserDefaults] setObject:date forKey:@"YourKey"];
于 2013-01-13T18:46:13.733 に答える