问题
I'm Using SAcalendar. this is my json:
data: - [
- {
id: "1",
event_start: "2014-06-09 11:30",
event_end: "2014-06-09 12:30",
title: "Click a date to create a new event and drag to change its date and time. Click on an existing event to modify. Click "Show Standard Settings" to set additional event properties."
},
- {
id: "2",
event_start: "2017-01-03 16:30",
event_end: "2017-01-03 17:30",
title: "fgdgf"
},
- {
id: "3",
event_start: "2017-01-11 03:30",
event_end: "2017-01-11 06:00",
title: "fdfgdg"
},]}
now when user click on particular date A alert will show with that event title. this is my logic
-(void) SACalendar:(SACalendar*)calendar didSelectDate:(int)day month:(int)month year:(int)year
{
for(int i=0; i<EventArray.count; i++)
{
if([[EventArray objectAtIndex:i]objectForKey:@"event_start"] && [[EventArray objectAtIndex:i]objectForKey:@"event_end"])
{
eventtitlestring = [[EventArray objectAtIndex:i] objectForKey:@"title"];
if(event_startString==eventtitlestring && event_endString==eventtitlestring)
{
UIAlertView *showevent = [[UIAlertView alloc]initWithTitle:@"Alert" message:self.eventtitlestring delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[showevent show];
}
}
}
plz help me with right logic i am new in ios,please help.. thanks in advance.
回答1:
From what I see in the question. In understood it as follows. You need to compare the selected date in calendar with dates given in json. If it matches, then you have to show the alert. For that, you need to compare the selected date with dates given in Json.
Inside the function,
-(void) SACalendar:(SACalendar*)calendar didSelectDate:(int)day month:(int)month year:(int)year
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd";
dateFormatter.timeZone = [NSTimeZone systemTimeZone];
for(int i=0; i < EventArray.count; i++)
{
NSString *eventStartString = [[EventArray objectAtIndex:i] objectForKey:@"event_start"];
NSString *eventEndString = [[EventArray objectAtIndex:i]objectForKey:@"event_end"];
if(eventStartString != nil && eventEndString != nil)
{// s_ -> start; e_ -> end;
eventStartString = [eventStartString substringToIndex:10];
eventEndString = [eventEndString substringToIndex:10];
NSDate *s_date = [dateFormatter dateFromString:eventStartString];
NSDate *e_date = [dateFormatter dateFromString:eventEndString]
NSCalendar *myCalendar = [NSCalendar currentCalendar];
long s_day = [myCalendar component:NSCalendarUnitDay fromDate:s_date];
long s_month = [myCalendar component:NSCalendarUnitMonth fromDate:s_date];
long s_year = [myCalendar component:NSCalendarUnitYear fromDate:s_date];
long e_day = [myCalendar component:NSCalendarUnitDay fromDate:e_date];
long e_month = [myCalendar component:NSCalendarUnitMonth fromDate:e_date];
long e_year = [myCalendar component:NSCalendarUnitYear fromDate:e_date];
eventtitlestring = [[EventArray objectAtIndex:i] objectForKey:@"title"];
if ((day == (int)s_day) && (month == (int)s_month) && (year == (int)s_year)) {
UIAlertView *showevent = [[UIAlertView alloc]initWithTitle:@"Alert" message:self.eventtitlestring delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[showevent show];
return;
}else if ((day == (int)e_day) && (month == (int)e_month) && (year == (int)e_year)) {
UIAlertView *showevent = [[UIAlertView alloc]initWithTitle:@"Alert" message:self.eventtitlestring delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[showevent show];
return;
}
}
}
}
After extracting month, day, year, you can compare them and accordingly show the event
来源:https://stackoverflow.com/questions/41691269/when-user-click-on-particular-date-a-alert-will-show-with-that-event-title