我使用这个自定义类EAActionSheetPicker
来创建和显示带有日期选择器和时间选择器的操作表。
下面的屏幕是此视图的情节提要:
所以这很简单——当我点击结束日期按钮时,它应该显示带有日期选择器的操作表。类似地,当我点击时间按钮时,它应该只显示时间选择器,但它会显示带有日期的选择器(与结束日期相同)。EAActionSheetPicker
但是在s 委托方法中发生了奇怪的事情: EAActionSheetPicker:didDismissWithSelection:inTextField:textField
- 我确定选择器是否处于日期或时间模式,并为我记录每个正确的值(下面的代码)。
- (IBAction)endDateSelectionButton:(UIButton *)sender
{
// Initialization of custom date picker
self.endDatePicker = [[EAActionSheetPicker alloc] initWithDatePickerMode:UIDatePickerModeDate];
// Setting style for action sheet
[self.endDatePicker setActionSheetStyle:UIActionSheetStyleBlackOpaque];
// Setting data mode for picker to be only date without time
[self.endDatePicker setDateMode:UIDatePickerModeDate];
// Uncomment this if you ever want a title
// [self.endDatePicker setTitle:@"Choose end date"];
self.endDatePicker.delegate = self;
// Showing in superview because of the tab bar controller
[self.endDatePicker showInView:[self.view superview]];
}
// Something doesn't work with time mode
- (IBAction)timeSelectionButton:(UIButton *)sender
{
self.timePicker = [[EAActionSheetPicker alloc] initWithDatePickerMode:UIDatePickerModeTime];
[self.timePicker setActionSheetStyle:UIActionSheetStyleBlackOpaque];
[self.timePicker setDateMode:UIDatePickerModeTime];
self.timePicker.delegate = self;
[self.timePicker showInView:[self.view superview]];
}
// Delegates method for getting picked date
- (void)EAActionSheetPicker:(EAActionSheetPicker *)actionSheet didDismissWithSelection:(id)selection inTextField:(UITextField *)textField
{
// Determining if action sheet date mode is date or time for new project options
if (actionSheet.dateMode == UIDatePickerModeDate) {
// Init date formatter for changing the date output for database
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// Setting data format to be: day-month-year
[formatter setDateFormat:@"dd-MM-yyyy"];
NSString *endDate = [formatter stringFromDate:selection];
self.endDate = selection;
// Setting button title to display selected date
[self.endDateButton setTitle:endDate forState:UIControlStateNormal];
} else if (actionSheet.dateMode == UIDatePickerModeTime) {
NSLog(@"TIME...");
}
}