设置放在Action表中的UIDatepicker的最小和最大日期

时间:2013-09-17 08:19:24

标签: ios uidatepicker uiactionsheet

我正在使用此类显示名为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];
}

configuredPickerView方法在库中,如下所示:

- (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;

}

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

2 个答案:

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

答案 1 :(得分:0)

您可以使用UIDatePicker属性: -

@property (nonatomic, retain) NSDate *minimumDate; // 
@property (nonatomic, retain) NSDate *maximumDate; // default is nil