我不明白为什么这段代码不再起作用了(它曾经工作过)。 我创建了一个UIDatePicker,它在指定日期触发日历中的提醒。我不知道为什么我会收到错误"没有设置日历"因为我确实设置了它(在代码中可以看到)
- (void)removeViews:(id)object {
[[self.view viewWithTag:9] removeFromSuperview];
[[self.view viewWithTag:10] removeFromSuperview];
[[self.view viewWithTag:11] removeFromSuperview];
}
- (void)dismissDatePicker:(id)sender {
if (eventStore == nil)
{
eventStore = [[EKEventStore alloc]init];
EKAuthorizationStatus authorizationStatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeReminder];
if (authorizationStatus == EKAuthorizationStatusNotDetermined) {
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if(granted){
[self createReminder];
}
}];
}
if (authorizationStatus == EKAuthorizationStatusAuthorized) {
[self createReminder];
}
}
if (eventStore != nil){
[self createReminder];
}
CGRect toolbarTargetFrame = CGRectMake(self.view.bounds.size.width/2-160, self.view.bounds.size.height/2-44-108, 320, 44);
CGRect datePickerTargetFrame = CGRectMake(self.view.bounds.size.width/2-160, self.view.bounds.size.height/2-108, 320, 216);
[UIView beginAnimations:@"MoveOut" context:nil];
[self.view viewWithTag:9].alpha = 0;
[self.view viewWithTag:10].frame = datePickerTargetFrame;
[self.view viewWithTag:11].frame = toolbarTargetFrame;
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(removeViews:)];
[UIView commitAnimations];
NSLog(@"dismiss");
}
- (IBAction)callDP:(id)sender {
if ([self.view viewWithTag:9]) {
return;
}
CGRect toolbarTargetFrame = CGRectMake(self.view.bounds.size.width/2-160, self.view.bounds.size.height/2-44-108, 320, 44);
CGRect datePickerTargetFrame = CGRectMake(self.view.bounds.size.width/2-160, self.view.bounds.size.height/2-108, 320, 216);
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(self.view.bounds.size.width/2-160, self.view.bounds.size.height+44, 320, 216)];
datePicker.tag = 10;
[datePicker setBackgroundColor:[UIColor whiteColor]];
datePicker.alpha = 0.87;
[self.view addSubview:datePicker];
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height+44, 320, 44)];
toolBar.tag = 11;
toolBar.barStyle = UIBarStyleDefault ;
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissDatePicker:)];
[toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]];
[self.view addSubview:toolBar];
[UIView beginAnimations:@"MoveIn" context:nil];
toolBar.frame = toolbarTargetFrame;
datePicker.frame = datePickerTargetFrame;
[UIView commitAnimations];
}
-(void)createReminder
{
EKReminder *reminder = [EKReminder
reminderWithEventStore:eventStore];
reminder.title = @"it's time to do your ELS exercise";
reminder.calendar = [eventStore defaultCalendarForNewReminders];
NSDate *date = [datePicker date];
EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:date];
[reminder addAlarm:alarm];
NSError *error = nil;
[eventStore saveReminder:reminder commit:YES error:&error];
if (error){
NSLog(@"error = %@", error);
}
}
我收到错误:
Domain=EKErrorDomain Code=1 "No calendar has been set." UserInfo=0x17826b400 {NSLocalizedDescription=No calendar has been set.}
答案 0 :(得分:1)
解决方案: 事件存储从未实例化,因为它不是零。 我在viewDidLoad中添加了:
eventStore = [[EKEventStore alloc]init];
我只在dismissDatePicker中保留以下内容:
[eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error) {
[self createReminder];
}];