我有一個日期選擇器視圖控制器,我選擇日期。當我回來時,我可以在桌面視圖單元上看到選定的日期。到目前爲止,這一切正常。當我退出應用程序並再次運行應用程序時,我遇到了一個問題,我看不到單元格中最後一個選定的值。我怎樣才能做到這一點?如何存儲選定的值並顯示在單元格上時應用程序再次運行iphone
如果用戶退出應用程序,他們應該能夠查看單元格中最後選擇的日期,以便他們可以查看或使用該日期。
這是我迄今爲止控制器類代碼.m文件
- (void)viewDidLoad
{
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, 0, 0)];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.minimumDate = [NSDate date];
[self.view addSubview:datePicker];
[super viewDidLoad];
}
-(void)DateChangeForFinalPayMent
{
//remove time
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
int comps = NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit;
NSDateComponents *dateComponents = [gregorian components:comps fromDate:[datePicker date]];
NSDate *dateSelected = [gregorian dateFromComponents:dateComponents];
NSLog(@"dateSelected:%@",dateSelected);
[gregorian release];
if ([selectionData type] == 0)
[selectionData setFromDateSelected:dateSelected];
else
[selectionData setToDateSelected:dateSelected];
}
-(IBAction)click
{
[self DateChangeForFinalPayMent];
[self.navigationController popViewControllerAnimated:YES];
}
這裏是我的firstView控制器在那裏我表明我的選擇交割日對細胞
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy"];
NSString* fromDate = [dateFormat stringFromDate:app.selectionData.fromDateSelected];
NSString* toDate = [dateFormat stringFromDate:app.selectionData.toDateSelected];
if ([indexPath row] == 0 && [indexPath section] == 1)
{
[email protected]"From Date";
cell.detailTextLabel.text=fromDate;
}
if ([indexPath row] == 1 && [indexPath section] == 1)
{
[email protected]"To Date";
cell.detailTextLabel.text=toDate;
}
可有人建議如何存儲值並在用戶再次打開應用程序時將其顯示給用戶。
在http://stackoverflow.com/questions/3074483/save-string-to-the-nsuserdefaults/3074489看看#3074489 – lluismontero
參考http://www.icodeblog.com/2008/10/03/iphone-programming-tutorial-savingretrieving-data-using-nsuserdefaults/ http://stackoverflow.com/questions/2013850/whats-the-最佳存儲方式 - nsdate-in-nsuserdefaults http://stackoverflow.com/questions/5663951/saving-a-date-in-nsuserdefaults-as-nsstring-not-working –