简体   繁体   中英

Getting Wrong Date While Using Date Picker

I took a baseViewController With a UIDatePicker.I am using this date picker in other viewControllers. When i am trying to get a date.always the previous date is showing up in the log.this is happening in every view controller.

getting a datepicker.

    self.datePicker = [[[UIDatePicker alloc] init] autorelease];
self.datePicker.datePickerMode = UIDatePickerModeDate;
[self.datePicker addTarget:self action:@selector(dateValueChanged:) forControlEvents:UIControlEventValueChanged];
[self.datePicker resignFirstResponder];

Taking the Value from a textfield using a date picker.

- (IBAction)dateValueChanged:(id)sender
{
UIDatePicker *picker = (UIDatePicker *)sender;
dateSelected = [picker date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
if (self.field.tag == 101)
    self.field.text = [dateFormatter stringFromDate:dateSelected];
NSLog(@"%@",dateSelected);
}

It looks as though you're editing the value of your entity but you are not persisting it. Try adding something like the following at the end of your save method:

NSError* error = nil;
[appDelegate.managedObjectContext save:&error];

作为测试,请尝试摆脱自动发布,并查看您的行为是否发生变化。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM