1

I have a date picker view controller where I select the date. When I come back I can see the selected date on the tableview cell. So far, this all works properly. I face a problem when I exit the application and run it again, I can't see the last selected value in the cell. How can I do this?

If the user exits the application, they should be able to see the last selected date on the cell so that they can review or use that date.

This is my date controller class code .m file

- (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];
}

here is my firstview controller where i show my selected value date on cell

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)
    {

        cell.textLabel.text=@"From Date";
        cell.detailTextLabel.text=fromDate;

    }   
    if ([indexPath row] == 1 && [indexPath section] == 1)
    {
        cell.textLabel.text=@"To Date";
        cell.detailTextLabel.text=toDate;
    }

Can anybody suggest how to store the value and show it to the user when they open the application again.

EmptyStack
  • 51,274
  • 23
  • 147
  • 178
Rocky
  • 1,427
  • 1
  • 28
  • 65
  • 1
    Take a look at http://stackoverflow.com/questions/3074483/save-string-to-the-nsuserdefaults/3074489#3074489 – llullulluis Oct 12 '11 at 09:37
  • refer http://www.icodeblog.com/2008/10/03/iphone-programming-tutorial-savingretrieving-data-using-nsuserdefaults/ http://stackoverflow.com/questions/2013850/whats-the-optimum-way-of-storing-an-nsdate-in-nsuserdefaults http://stackoverflow.com/questions/5663951/saving-a-date-in-nsuserdefaults-as-nsstring-not-working – Nikunj Jadav Oct 12 '11 at 10:24

1 Answers1

1

PFB code to save and reteive the date value from the User defaults

//Storing date value to the  user defaults 

NSDate * selectedDate = [NSDate date]; 

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 

//Set the date format.. u can choose different one based on ur requirement
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 

//String from the NSDate
NSString *pStrDate = [dateFormatter stringFromDate:selectedDate];

//Save date value to the user defaults
 [[NSUserDefaults standardUserDefaults] setObject: pStrDate forKey:@"date"]; 

 [[NSUserDefaults standardUserDefaults] synchronize];


Retrieving date from the user defaults: 
NSDate *dateFromString = [[NSDate alloc] init]; 
NSString *savedDateVaue=nil; 

if([[NSUserDefaults standardUserDefaults] objectForKey:@"date"]!=nil)
{
 savedDateVaue=[[NSUserDefaults standardUserDefaults] objectForKey:@"date"];
 dateFromString = [dateFormatter dateFromString:savedDateVaue];  
}
Saikiran Komirishetty
  • 6,525
  • 1
  • 29
  • 36
  • ksk i try but it not working can change in my code how to do that please help me – Rocky Oct 12 '11 at 11:06
  • //Storing date in user defaults NSDate * selectedDate = [NSDate date]; NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSString *pStrDate = [dateFormatter stringFromDate:selectedDate]; [[NSUserDefaults standardUserDefaults] setObject: pStrDate forKey:@"date"]; Retrieving date from the user defaults: NSDate *dateFromString = [[NSDate alloc] init]; NSString *pDate=[[NSUserDefaults standardUserDefaults] objectForKey:@"date"]; dateFromString = [dateFormatter dateFromString:pDate]; – Saikiran Komirishetty Oct 12 '11 at 12:05