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];
poDate
is an instance of UIPopoverController
, while datePicker
is a variable typed UIDatePicker
, which is why you get a warning on that line.
So on this line (where the program crashes):
NSString *dt = [df stringFromDate:datePicker.date];
The date
message is sent to datePicker
, which is actually an instance of UIPopoverController
. Popover controllers don't have a date
method, so the program crashes.
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.