EKCalendarEKCalendar CGcolor未在iPhone的日历中设置颜色 [英] EKCalendarEKCalendar CGcolor is not setting color in iPhone's calendar

查看:117
本文介绍了EKCalendarEKCalendar CGcolor未在iPhone的日历中设置颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种类型的事件,所以我设置了两种不同的颜色来区分日历中的事件,但我无法设置EkCalendar的颜色,这里是我的代码

i have two type of events,so i have set two different color to differentiate between events in calendar,but i am not able to set color of EkCalendar, here is my code

NSDateComponents* deltaComps = [[NSDateComponents alloc] init];
//[deltaComps setDay:3];
[deltaComps setMinute:02];
NSDate* tomorrow = [[NSCalendar currentCalendar] dateByAddingComponents:deltaComps toDate:[NSDate date] options:0];
//NSDate* dayAfterTomorrow = [[NSCalendar currentCalendar] dateByAddingComponents:deltaComps toDate:tomorrow options:0];
// You can use the event store now
EKCalendar *cal;
EKEvent *myEvent  = [EKEvent eventWithEventStore:eventDB];
myEvent.title     = @"Visit1";
myEvent.startDate = tomorrow;
myEvent.endDate   = tomorrow;
myEvent.allDay = NO;
myEvent.notes = @"Visit 5:30p - 8:30p";
cal.CGColor=[UIColor yellowColor].CGColor  ;
//myEvent.
//[myEvent setCalendar:[eventDB defaultCalendarForNewEvents]];
cal = [EKCalendar calendarForEntityType:EKEntityMaskEvent eventStore:eventDB];
//[myEvent setCalendar:cal];
[myEvent setCalendar:[eventDB defaultCalendarForNewEvents]];
EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:1.0f];
[myEvent addAlarm:alarm];

NSError *err;
//[eventDB saveEvent:myEvent span:EKSpanFutureEvents error:&err];
[eventDB saveEvent:myEvent span:EKSpanThisEvent commit:YES error:&err];


NSDateComponents* deltaComps1 = [[NSDateComponents alloc] init];
[deltaComps1 setDay:2];
NSDate* tomorrow1 = [[NSCalendar currentCalendar] dateByAddingComponents:deltaComps1 toDate:[NSDate date] options:0];
//NSDate* dayAfterTomorrow1 = [[NSCalendar currentCalendar] dateByAddingComponents:deltaComps toDate:tomorrow1 options:0];



// 2nd event
EKEvent *myEvent2  = [EKEvent eventWithEventStore:eventDB];
myEvent2.title     = @"Visi1";
myEvent2.startDate = tomorrow1;
myEvent2.endDate   = tomorrow1;
myEvent2.allDay = NO;
myEvent2.notes = @"Visit 9:00a - 8:30p";
[myEvent2 setCalendar:[eventDB defaultCalendarForNewEvents]];
EKAlarm *alarm1 = [EKAlarm alarmWithRelativeOffset:1.0f];

[myEvent2 addAlarm:alarm1];
[eventDB saveEvent:myEvent2 span:EKSpanThisEvent commit:YES error:&err];
if (err) {
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat:@"%@", err] delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
    [alert show];
}else {
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setBool:YES forKey:@"EVENTS_CREATED"];
    [prefs synchronize];
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Visit events has been added to calendar." delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
    [alert show];
}


推荐答案

你正在设置 CGColor未初始化对象上的属性:

You're setting the "CGColor" property on an uninitialized object:

EKCalendar *cal;
[...]
cal.CGColor=[UIColor yellowColor].CGColor  ;
cal = [EKCalendar calendarForEntityType:EKEntityMaskEvent eventStore:eventDB];

您应该在设置颜色的调用之前移动日历初始化。

You should move the calendar initialization before the calls that sets the color.

此外,我不确定这是否可行。在尝试之前检查calendar.immutable是否返回false。我认为,对于用户应用程序创建的日历,无权更改颜色,否则您可以运行应用程序,所有日历都可以更改颜色...

In addition, I'm not sure this is even possible. Check that calendar.immutable returns false before trying this. I think that for the calendars created by the user apps don't have the right to change the color otherwise you could run an app and all your calendars could change colors...

我认为如果你想用你自己的自定义颜色设置一个日历,你可能需要自己创建一个。例如,参见本教程

I think if you want to set up a calendar with your own custom color you might have to create one yourself. See for example this tutorial

这篇关于EKCalendarEKCalendar CGcolor未在iPhone的日历中设置颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆