【问题标题】:Disable selection to a range of dates in FSCalendar ObjC在 FSCalendar ObjC 中禁用对一系列日期的选择
【发布时间】:2018-08-30 04:57:03
【问题描述】:

我是 ObjC 的新手,我正在使用 FSCalendar 库。我需要禁用对一系列日期的选择。例如:从日期 - 2018-01-01 到日期 - 2018-12-30。我使用以下方法禁用过去的日期。

- (BOOL)calendar:(FSCalendar *)calendar shouldSelectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition

请帮我禁用日期范围的选择

【问题讨论】:

    标签: objective-c cocoapods fscalendar


    【解决方案1】:

    我对 FSCalendar 不是很熟悉,但我快速浏览了一下,您应该能够设置 startDateToAvoid 和 endingDateToAvoid,然后检查从 shouldSelectDate 传入的日期是否在该范围内,如果是则返回 NO (禁止选择):

    #import "ViewController.h"
    #import "FSCalendar.h"
    
    @interface ViewController () <FSCalendarDelegate, FSCalendarDataSource>
    @property (strong, nullable) NSDate *startingDateToAvoid;
    @property (strong, nullable) NSDate *endingDateToAvoid;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyy-MM-dd";
        self.startingDateToAvoid = [formatter dateFromString:@"2018-01-01"];
        self.endingDateToAvoid = [formatter dateFromString:@"2018-12-30"];
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (BOOL)_shouldAllowSelectionOfDate:(NSDate *)date {
        // if you want it to be inclusive of the starting/ending dates (i.e. they can't select 2018-01-01 as well) then uncomment this line below
        /*if ([date isEqualToDate:self.startingDateToAvoid] || [date isEqualToDate:self.endingDateToAvoid]) {
            return NO;
        }*/
        // if the date passed in is between your starting and ending date, we don't want to allow selection
        if ([date compare:self.startingDateToAvoid] == NSOrderedDescending &&
            [date compare:self.endingDateToAvoid] == NSOrderedAscending) {
            return NO;
        }
        return YES;
    }
    
    - (BOOL)calendar:(FSCalendar *)calendar shouldSelectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition {
        return [self _shouldAllowSelectionOfDate:date];
    }
    
    @end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-10
      • 1970-01-01
      • 2020-08-25
      • 1970-01-01
      • 2020-07-15
      • 1970-01-01
      • 2017-03-02
      相关资源
      最近更新 更多