1

我正在使用此类显示名为 Action sheet picker的日期选择器,我遇到的问题是我无法设置选择器的最大和最小日期。

我正在使用的代码是这样的:

在 tableview didSelectRow 方法中:

  else if (indexPath.row == 2){
        // here I am using a library which successfully shows a date picker from where we can pick the dates on which we want to request the appt.
        _actionSheetPicker = [[ActionSheetDatePicker alloc] initWithTitle:@"" 
      datePickerMode:UIDatePickerModeTime selectedDate:self.selectedDateandTime 
   target:self action:@selector(dateWasSelected:element:) origin:[self.tableView
  cellForRowAtIndexPath:indexPath]];
        [self.actionSheetPicker addCustomButtonWithTitle:[self.selectedDateandTime 
      getDateStringInFormat:@"dd MMM yyyy"] value:Nil];
        UIDatePicker * picker = (UIDatePicker *)[self.actionSheetPicker 
     configuredPickerView];
        [picker setMinimumDate:self.selectedDateandTime];

        self.actionSheetPicker.hideCancel = NO;
        [self.actionSheetPicker showActionSheetPicker];
}

配置的PickerView 方法在库中,如下所示:

- (UIView *)configuredPickerView {
CGRect datePickerFrame = CGRectMake(0, 40, self.viewSize.width, 216);
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:datePickerFrame];
datePicker.datePickerMode = self.datePickerMode;
[datePicker setDate:self.selectedDate animated:NO];


[datePicker addTarget:self action:@selector(eventForDatePicker:) forControlEvents:UIControlEventValueChanged];

//need to keep a reference to the picker so we can clear the DataSource / Delegate when dismissing (not used in this picker, but just in case somebody uses this as a template for another picker)
self.pickerView = datePicker;

return datePicker;

}

请如果有人可以帮助我解决这个问题。如果需要任何额外的帮助来理解课程的架构,那么请随时问我。

4

2 回答 2

1

我遇到了同样的问题,所以我在ActionSheetPicker-3.0中写了一个方法

将此添加到您的ActionSheetPicker-3.0库中。

// ActionSheetDatePicker.h
+ (id)showPickerWithTitle:(NSString *)title datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate minimumDate:(NSDate *)minimumDate maximumDate:(NSDate *)maximumDate target:(id)target action:(SEL)action origin:(id)origin;


// ActionSheetDatePicker.m
+ (id)showPickerWithTitle:(NSString *)title datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate minimumDate:(NSDate *)minimumDate maximumDate:(NSDate *)maximumDate target:(id)target action:(SEL)action origin:(id)origin;
{
    ActionSheetDatePicker *picker = [[ActionSheetDatePicker alloc] initWithTitle:title datePickerMode:datePickerMode selectedDate:selectedDate target:target action:action origin:origin];
    picker.minimumDate = minimumDate;
    picker.maximumDate = maximumDate;
    [picker showActionSheetPicker];
    return picker;
}

在要使用 ActionSheetDatePicker 的地方编写此代码

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *maximumDateComponents = [calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
    [maximumDateComponents setYear:2050];
    NSDate *minDate = [NSDate date];
    NSDate *maxDate = [calendar dateFromComponents:maximumDateComponents];


[ActionSheetDatePicker showPickerWithTitle:@"Select Start Date" datePickerMode:UIDatePickerModeDate selectedDate:[NSDate date] minimumDate:minDate maximumDate:maxDate target:self action:@selector(dateSelected:element:) origin:sender];
于 2015-05-26T09:14:21.830 回答
0

您可以使用 UIDatePicker 属性:-

@property (nonatomic, retain) NSDate *minimumDate; // 
@property (nonatomic, retain) NSDate *maximumDate; // default is nil
于 2014-08-31T11:00:14.560 回答