代码之家  ›  专栏  ›  技术社区  ›  Catherine

在FSCalendar ObjC中禁用对日期范围的选择

  •  0
  • Catherine  · 技术社区  · 6 年前

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

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

    1 回复  |  直到 6 年前
        1
  •  0
  •   R4N    6 年前

    我不太熟悉FSCalendar,但我快速查看了一下,您应该能够设置startingDateToAvoid和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