不兼容的指针类型分配给' UIDatePicker *'来自' UIPopoverController *'

时间:2014-08-08 16:16:53

标签: ios objective-c uipopovercontroller uidatepicker

我正在尝试调整此帖子中的第一个答案:How can I show a UIDatePicker inside a Popover on iPad using StoryBoard?

这是我改编的代码(datePicker在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];
}

然而,有两个问题。首先,我在最后一行收到编译器警告:

datePicker = poDate;

错误是:

Incompatible pointer types assigning to 'UIDatePicker *' from 'UIPopoverController *' 

其次,应用程序在解除弹出窗口时崩溃,错误为:

unrecognized selector sent to instance

应用程序在popoverControllerDidDismissPopover方法中的此行停止:

NSString *dt = [df stringFromDate:datePicker.date];

1 个答案:

答案 0 :(得分:0)

poDateUIPopoverController的实例,而datePicker是变量类型UIDatePicker,这就是您在该行上收到警告的原因。

所以在这一行(程序崩溃的地方):

NSString *dt = [df stringFromDate:datePicker.date];

date消息被发送到datePickerUIPopoverController实际上是date的实例。 Popover控制器没有{{1}}方法,因此程序崩溃。