我的APP存在问题。我需要保存Datapicker选择。
我有2个textfield 1标签和1个datapicker。文本字段中的“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
答案 0 :(得分:0)
NSDate也是一个属性列表对象,因此它可以以相同的方式存储:
[[NSUserDefaults standardUserDefaults] setObject:date forKey:@"YourKey"];