开发者

how to store selected value and show on cell when application run again in iphone

开发者 https://www.devze.com 2023-04-12 16:31 出处:网络
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 appl

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 sh开发者_Python百科ow it to the user when they open the application again.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号