I am trying to adapt the first answer in this post: How can I show a UIDatePicker inside a Popover on iPad using StoryBoard?
Here is my adapted code (datePicker is initialised in viewDidLoad):
CGRect pickerFrame = CGRectMake(0, 0, 320, 216);
datePicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
datePicker.datePickerMode = UIDatePickerModeDate;
- (IBAction)btnSurveyDate:(UIButton *)sender
{
//Build custom popover view.
UIView *v = [[UIView alloc] init];
[v addSubview:datePicker];
UIViewController *popoverContent = [[UIViewController alloc] init];
popoverContent.view = v;
//Resize the popover view shown in the current view to the view's size.
popoverContent.preferredContentSize = CGSizeMake(320, 216);
//Create a popover controller with my DatePickerViewController in it.
UIPopoverController *poDate = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
//Set the delegate to self to receive the data of the DatePicker in the popover.
poDate.delegate = self;
//Present the popover.
[poDate presentPopoverFromRect:self.txtSurveyDate.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
datePicker = poDate;
}
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd/MM/yyyy"];
NSString *dt = [df stringFromDate:datePicker.date];
self.txtSurveyDate.text = [NSString stringWithFormat:@"%@",dt];
}
However, there are two problems. First, I get a compiler warning on the last line:
datePicker = poDate;
The error is:
Incompatible pointer types assigning to 'UIDatePicker *' from 'UIPopoverController *'
Second, the app crashes when dismissing the popover, with the error:
unrecognized selector sent to instance
The app stops at this line in the popoverControllerDidDismissPopover method:
NSString *dt = [df stringFromDate:datePicker.date];