EKCalendarEKCalendar CGcolor is not setting color in iPhone's calendar

吃可爱长大的小学妹 提交于 2019-12-11 08:20:02

问题


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

回答1:


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.

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



来源:https://stackoverflow.com/questions/22325352/ekcalendarekcalendar-cgcolor-is-not-setting-color-in-iphones-calendar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!