簡體   English   中英

EKReminder尚未設置日歷

[英]EKReminder No calendar has been set

我不明白為什么該代碼不再起作用(它曾經起作用)。 我創建一個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.}

解決方案:事件存儲從未實例化,因為它不是nil。 我在viewDidLoad中添加了:

eventStore = [[EKEventStore alloc]init];

並且我在dismissDatePicker中僅保留以下內容:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM