当用户单击特定日期时,将显示带有该事件标题的警报
Posted
技术标签:
【中文标题】当用户单击特定日期时,将显示带有该事件标题的警报【英文标题】:when user click on particular date a alert will show with that event title 【发布时间】:2017-01-17 07:28:17 【问题描述】:我正在使用 SAcalendar。 这是我的 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"
,]
现在,。 这是我的逻辑
-(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];
请帮助我正确的逻辑 我是ios新手,请帮忙。。 提前致谢。
【问题讨论】:
为什么要在这里添加for循环? 为什么要将日期字符串event_start
与event_title
进行比较?
eventtitlestring 是带有“title”的字符串 show json..
因为 SACalendar 的这个委托方法“委托方法:选择日期时调用”@User511
这是我的逻辑,我是ios新手,所以我的逻辑很弱,请你建议我该怎么办?
【参考方案1】:
从我在问题中看到的。理解如下。您需要将日历中的选定日期与 json 中给出的日期进行比较。如果匹配,则必须显示警报。为此,您需要将所选日期与 Json 中给出的日期进行比较。
函数内部,
-(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;
提取月、日、年后,可以进行比较并相应显示事件
【讨论】:
得到这个错误 -[NSCalendar component:fromDate:]: date cannot be nil 未来异常。此投诉将报告其中一些错误,然后将忽略进一步的违规行为。这是这次发生这种情况的回溯(由于编译器优化,某些帧可能会丢失): s_day 返回 nil; 让我检查一下并回复你 dateFormatter 也是 "yyyy-mm-dd" 请注意。 女士你摇滚...完成,我刚刚更改 dateFormatter.dateFormat = @"yyyy-MM-DD HH:mm";在旧的.. 谢谢你,妈妈,真是你的天才:)以上是关于当用户单击特定日期时,将显示带有该事件标题的警报的主要内容,如果未能解决你的问题,请参考以下文章
将 MKMapView 移动到相对于 superview 的特定位置
如何调用参数来自 Ruby 模型的 javascript 函数?