Reputation: 117
So I have a method where I change the keyboard to a UIDatePicker, and I want the user to select the date to set a reminder. So far I have the keyboard swap to a UIDatePicker, and when I print out the date string it only prints the current date, regardless of what I set.
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]);
Upvotes: 1
Views: 1715
Reputation: 12344
Default date of UIDatePicker is the current date and that is what you are printing. For picked date use
[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);
}
Upvotes: 3