0

我目前有一个DatePickerViewController我在情节提要中创建的具有 UIDatePicker 的弹出框。我希望能够关闭弹出窗口并将日期发送到我的原始视图ViewController,但我不确定如何执行此操作。我知道如何从 UIDatePicker 获取日期,只是不知道如何在弹出框被解除时获取该数据。

我正在尝试这个,ViewController但从未显示 NSLog 输出......

- (void) popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
    NSDate *selectedDate = ((DatePickerViewController *) popoverController.contentViewController).datePicker.date;
    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:selectedDate];
    int selectedYear = [dateComponents year];
    int selectedMonth = [dateComponents month];
    int selectedDay = [dateComponents day];

    self.dateLabel.text = [NSString stringWithFormat:@"%d/%d/%d",selectedMonth,selectedDay,selectedYear];

    NSLog(@"hello world"); // never shows up
}
4

1 回答 1

0

我必须在 prepareForSegue 中设置 delegate=self:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"toDatePicker"]) {
    UIStoryboardPopoverSegue *popoverSegue;
    popoverSegue = (UIStoryboardPopoverSegue *)segue;

    UIPopoverController *popoverController;
    popoverController = popoverSegue.popoverController;
    popoverController.delegate=self;
}
}
于 2012-08-30T00:09:21.800 回答