如何使用THCalendarDatePicker将选择日期设置为特定日期(30天)



我使用THCalendarDatePicker作为我的应用程序的日历,但是,我需要只允许从当前日期选择最多30天。

有人有什么想法吗?我只看到了"禁用未来选择",在每一个即将到来的日子切换后,无法选择。

这看起来不像是THCalendarDatePicker的一个特性。您可能只需要子类化它或提交一个pull请求来添加此功能。

查看THDatePickerViewController.m中的- (BOOL)dateInFutureAndShouldBeDisabled:(NSDate *)dateToCompare方法

您应该能够修改它,以便在决定是否启用日期时考虑可配置的偏移量(例如30天)。

这是一个快速修改的版本,做你正在寻找的:

- (BOOL)dateInFutureAndShouldBeDisabled:(NSDate *)dateToCompare {
NSDate *currentDate = [self dateWithOutTime:[NSDate date]];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger comps = (NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear);
//QUICK AND DIRTY MODIFIED CODE. daysInTheFutureThatAreSelectable should be a configurable property.
NSInteger daysInTheFuture;
NSInteger daysInTheFutureThatAreSelectable = 30;
daysInTheFuture = [[[NSCalendar currentCalendar] components: NSCalendarUnitDay
                                        fromDate: currentDate toDate: dateToCompare options: 0] day];
if (daysInTheFuture > 0 && daysInTheFuture <= daysInTheFutureThatAreSelectable) {
    return false;
}
//////////////////////////////////////
currentDate = [calendar dateFromComponents:[calendar components:comps fromDate:currentDate]];
dateToCompare = [calendar dateFromComponents:[calendar components:comps fromDate:dateToCompare]];
NSComparisonResult compResult = [currentDate compare:dateToCompare];
return (compResult == NSOrderedDescending && _disableHistorySelection) || (compResult == NSOrderedAscending && _disableFutureSelection);
}

最新更新