0

我有这个APP的问题。我需要保存一个 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 回答