1

I am using Tapku library for calendar implementation. I could see that there is a way to add Markers for predefined start and end date but I want to allow users to select/unselect any number of dates from current month only, and want to generate event for each action.

Moreover, I have switched off the month navigation functionality by returning nil for Left and Right arrow to display only current month but not able to remove events for few previous and next months Date tiles that gets displayed on current month. I can still select previous month's day 31st to navigate to previous month or select 1st on next month to navigate to next month. Can I restrict the date selection to only current month please?

Thanks.

Community
  • 1
  • 1
Paresh Masani
  • 7,474
  • 12
  • 73
  • 139

3 Answers3

2

The touches are handled in TKCalendarMonthView.m in the following method:

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

look at the block at row 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;
}

The selecting/deselecting perhaps doesn't work as you expect. It's not like setDateSelected and setDateDeselected.. instead there is a single UIImageView*, which represents the selected state. And that view is moved around to the current position. You can search for self.selectedImageView in the code to see, what is happening.

So its not that easy to introduce multiple-date-selection. The architecture isn't built for that.

calimarkus
  • 9,955
  • 2
  • 28
  • 48
  • Hi Jaydee, the if(down) code never executes! I kept NSLog and it never comes there while selection or unselection dates! – Paresh Masani Mar 14 '12 at 09:34
  • did you already add the return statement!? it should be executed if you select days from the previous or the following month. – calimarkus Mar 14 '12 at 09:37
  • Oh all right. Yah that worked! Thanks. I am also looking for allowing users to select multiple dates. Could you please tell me where is selecting date, changing tile image for selected and previous selected date handled? – Paresh Masani Mar 14 '12 at 09:54
  • I added additional informations in my answer - in short: that's not possible without refactoring pretty much of the code (and the actual architecture). – calimarkus Mar 14 '12 at 10:22
  • Thanks Jaydee. I will have to support multiple dates selection. I will implement! Thanks again for the information you provided. Very useful! – Paresh Masani Mar 14 '12 at 11:38
  • Hi Jaydee, I have done this other way whenever user selects date I display next sever days entries. Client is happy with it! :-) I see the tiny problem with GUI design - could you please see this - http://stackoverflow.com/questions/9719633/ios-tapku-calendar-library-set-tiles-grid-6x6-always Thanks. – Paresh Masani Mar 17 '12 at 21:17
  • Hi Jaydee, I have started bounty on my question - http://stackoverflow.com/questions/9719633/ios-tapku-calendar-library-enhancements – Paresh Masani Mar 30 '12 at 09:41
2

In TKCalendarMonthView there is a method name

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

in that method comment this line

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

this wont allow you to change month. You can store all selected date in an array and pass all the values in

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

The above method is used to put tiles but if u want selection image then u can replace it with tile image

Dhara
  • 4,093
  • 2
  • 36
  • 69
  • Yah this one is soted out. Do you know if there is anyway to implement multiple date select/deselect? Please refer to @Jaydee3's ans. Thanks. – Paresh Masani Mar 14 '12 at 11:41
  • @AppleDeveloper you want to select 1 date at a time or more? – Dhara Mar 14 '12 at 11:42
  • I want to select and deselect multiple dates one by one in any order from current month and needs to generate event for each select/deselect action. Now when you select another date, it removes Blue image from previously selected date. I want to keep it until it has selected again to deselect it! – Paresh Masani Mar 14 '12 at 11:43
  • you can use this method - (void)calendarMonthView:(TKCalendarMonthView *)monthView didSelectDate:(NSDate *)d for selecting date u will get the selected date here – Dhara Mar 14 '12 at 11:51
  • 1
    It wont execute because ur using wrong method use void)calendarMonthView:(TKCalendarMonthView *)monthView didSelectDate:(NSDate *)d – Dhara Mar 14 '12 at 12:29
  • That's true! I think the last thing I am struggling to get done is switching on/off selectedImageView for select Date tile. I just want to have a blue tile whenever date selected and want to persist it until that date selected again! Could you brief me at least how could I do this please. Thanks. – Paresh Masani Mar 14 '12 at 13:39
  • Hi Dhara, I have started bounty on my question - http://stackoverflow.com/questions/9719633/ios-tapku-calendar-library-enhancements – Paresh Masani Mar 30 '12 at 09:41
0

You can also try this code:

You can do this by first entering the dates in to an array. code for this is.

- (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;
}

}

After this you have to use a button click action by which you can make the dates selected between these two dates. Code for it is:

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

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

}

}

Then in one delegate method you just have to make an array containing all the dates between these two dates. It will be called just after the button click. Code for it is:

 - (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];

}

I hope, this helped you. Please let me know if you face any problem.

Ashutosh
  • 2,215
  • 14
  • 27