【问题标题】:EKReminder No calendar has been setEKReminder 没有设置日历
【发布时间】:2014-04-08 19:30:43
【问题描述】:

我不明白为什么这段代码不再起作用(它曾经起作用)。 我创建了一个 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.}

【问题讨论】:

    标签: ios uikit uidatepicker


    【解决方案1】:

    解决方案: 事件存储从未实例化,因为它不是 nil。 我在 viewDidLoad 中添加了:

    eventStore = [[EKEventStore alloc]init];
    

    并且我在 dismissDatePicker 中只保留了以下内容:

    [eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error) {
            [self createReminder];
        }];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-24
      • 1970-01-01
      相关资源
      最近更新 更多