如何在解除弹出窗口后确定从UIDatePicker中选择的日期?

时间:2012-08-29 23:18:43

标签: objective-c xcode uidatepicker uistoryboard

我目前在故事板中创建了一个带有UIDatePicker的popover DatePickerViewController。我希望能够解除弹出窗口并将日期发送到我的原始视图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
}

1 个答案:

答案 0 :(得分: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;
}
}