UIDatePicker returns 当前日期,即使我选择了不同的日期

UIDatePicker returns current date, even when I pick a different date

所以我有一个方法可以将键盘更改为 UIDatePicker,并且我希望用户select 设置提醒的日期。到目前为止,我已经将键盘切换到 UIDatePicker,当我打印出日期字符串时,它只打印当前日期,而不管我设置了什么。

self.datePicker = [[UIDatePicker alloc] init];

[self.noteTextView becomeFirstResponder];
[self.noteTextView setInputView:self.datePicker];

NSDate *date = self.datePicker.date;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeZone:NSDateFormatterNoStyle];
[dateFormatter setLocale:[NSLocale currentLocale]];

self.myDate = [dateFormatter stringFromDate:date]; //a string of the date that I want to use in other methods

NSLog(@"Date: %@", [dateFormatter stringFromDate:date]);

UIDatePicker 的默认日期是当前日期,这就是您要打印的日期。对于选择的日期使用

[self.myDatePicker addTarget:self action:@selector(datePickerChanged:) forControlEvents:UIControlEventValueChanged];

- (void)datePickerChanged:(UIDatePicker *)datePicker
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm"];
NSString *strDate = [dateFormatter stringFromDate:datePicker.date];
NSLog(@"Date: %@", strDate);
}