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

时间:2015-09-03 03:22:10

标签: ios

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

有没有人有任何想法?我只看到"禁用未来的选择",在切换到每个即将到来的日子时都无法选择。

1 个答案:

答案 0 :(得分:0)

看起来这不是THCalendarDatePicker的功能。您可能只需要对其进行子类化或提交拉取请求以添加此功能。

查看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);
}