我目前在故事板中创建了一个带有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
}
答案 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;
}
}