1

我正在使用Tapku库来实现日历。我可以看到有一种方法可以为预定义的开始和结束日期添加标记,但我希望允许用户仅从当前月份选择/取消选择任意数量的日期,并希望为每个操作生成事件。

此外,我通过为左箭头和右箭头返回 nil 来关闭月份导航功能,以仅显示当前月份,但无法删除前几个月和下个月的事件 显示在当前月份的日期图块。我仍然可以选择上个月的第 31 天导航到上个月,或者选择下个月的第 1 天导航到下个月。我可以将日期选择限制为仅当月吗?

谢谢。

4

3 回答 3

2

TKCalendarMonthView.m通过以下方法处理触摸:

- (void) reactToTouch:(UITouch*)touch down:(BOOL)down

查看第 563 行的块:

if(portion == 1)
{
    selectedDay = day;
    selectedPortion = portion;
    [target performSelector:action withObject:[NSArray arrayWithObject:[NSNumber numberWithInt:day]]];
}
else if(down)
{
    // this is the important part for you.
    // ignore it by adding a return here (or remove the following three lines)
    return;
    [target performSelector:action withObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:day],[NSNumber numberWithInt:portion],nil]];
    selectedDay = day;
    selectedPortion = portion;
}

选择/取消选择可能不像您期望的那样工作。它不像setDateSelectedand setDateDeselected.. 而是有一个UIImageView*, 代表选定的状态。并且该视图被移动到当前位置。您可以在代码中搜索self.selectedImageView以查看发生了什么。

所以引入多日期选择并不容易。该架构不是为此而构建的。

于 2012-03-13T16:16:22.470 回答
2

里面有TKCalendarMonthView一个方法名

-(void) reactToTouch:(UITouch*)touch down:(BOOL)down 

在该方法中注释此行

[target performSelector:action withObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:day],[NSNumber numberWithInt:portion],nil]];

这不会让你改变月份。您可以将所有选定的日期存储在一个数组中并将所有值传递给

- (NSArray*)calendarMonthView:(TKCalendarMonthView *)monthView marksFromDate:(NSDate *)startDate toDate:(NSDate *)lastDate 

上述方法用于放置图块,但是如果您想要选择图像,则可以将其替换为图块图像

于 2012-03-14T11:33:10.680 回答
0

你也可以试试这段代码:

您可以通过首先将日期输入到数组中来做到这一点。代码是。

- (void)calendarMonthView:(TKCalendarMonthView *)monthView didSelectDate:(NSDate *)d {
NSLog(@"selected Date IS - %@",inDate);

[myArray addObject:d];

for (id entry in myArray)
{

    if (inDate == nil && outDate == nil)
    {
        inDate = d;
        outDate = d;
    }
    if ([d compare:inDate] == NSOrderedAscending)
    {
        inDate = d;
    }
    if ([d compare:outDate] == NSOrderedDescending)
    {
        outDate = d;
    }

    d = nil;
}

}

在此之后,您必须使用按钮单击操作,通过它您可以在这两个日期之间选择日期。它的代码是:

 - (IBAction)goBtn:(id)sender
  {
NSLog(@"startDate is: %@",inDate);
NSLog(@"endDate is: %@",outDate);

[calendar reload];
inDate = nil;
outDate = nil;

}

}

然后在一个委托方法中,您只需创建一个包含这两个日期之间所有日期的数组。它将在按钮单击后被调用。它的代码是:

 - (NSArray*)calendarMonthView:(TKCalendarMonthView *)monthView marksFromDate:(NSDate *)startDate toDate:(NSDate *)lastDate {
//***********
NSMutableArray *tempData = [[NSMutableArray alloc] init];
NSDate *nextDate;
for ( nextDate = inDate ; [nextDate compare:outDate] < 0 ; nextDate = [nextDate addTimeInterval:24*60*60] ) {
    // use date
    NSLog(@"%@",nextDate);
    [tempData addObject:[NSString stringWithFormat:@"%@",nextDate]];
}
[tempData addObject:[NSString stringWithFormat:@"%@",outDate]];
//***********


NSMutableArray *marks = [NSMutableArray array];


NSCalendar *cal = [NSCalendar currentCalendar];
[cal setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

NSDateComponents *comp = [cal components:(NSMonthCalendarUnit | NSMinuteCalendarUnit | NSYearCalendarUnit |
                                          NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSSecondCalendarUnit)
                                fromDate:startDate];
NSDate *d = [cal dateFromComponents:comp];

NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:1];


while (YES) {
    if ([d compare:lastDate] == NSOrderedDescending) {
        break;
    }

    if ([tempData containsObject:[d description]]) {
        [marks addObject:[NSNumber numberWithBool:YES]];
    } else {
        [marks addObject:[NSNumber numberWithBool:NO]];
    }

    d = [cal dateByAddingComponents:offsetComponents toDate:d options:0];
}

return [NSArray arrayWithArray:marks];

}

我希望,这对你有帮助。如果您遇到任何问题,请告诉我。

于 2013-05-06T09:52:29.007 回答