0

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];
Community
  • 1
  • 1
eby
  • 191
  • 2
  • 12

1 Answers1

0

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.

Austin
  • 5,625
  • 1
  • 29
  • 43
  • Thanks, I had a feeling the crash was happening because of the incompatible object types. This error is present in the original version that I linked to. Does anyone know how to fix it? When I remove that line, the app crashes when the button is pressed - error: uncaught exception 'NSGenericException', reason: '-[UIPopoverController dealloc] reached while popover is still visible'. Leaving the line in, the code runs, and the popup appears and works until it's dismissed. – eby Aug 08 '14 at 16:28
  • You need to create a new `strong` / `retain` property especially for the popover. – Austin Aug 08 '14 at 16:38