使用 EKEventEditViewController 创建带有闹钟的日历事件

发布于 2024-12-10 01:58:24 字数 1177 浏览 0 评论 0原文

我使用以下代码创建一个事件,并显示一个弹出窗口,要求用户保存该事件:

EKEventStore *eventStore = [[EKEventStore alloc] init];
EKCalendar *calendar = [eventStore defaultCalendarForNewEvents];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.calendar = calendar;
event.title = [NSString stringWithFormat:@"Event: %@", [self.event title]]; 
event.location = self.event.location;
event.notes = [self stringByStrippingHTML: [self.event description]];
event.startDate = [self.event startDate]; 
event.endDate = [self.event endDate];

NSTimeInterval alarmOffset = -1*60*60;//1 hour
EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:alarmOffset];

[event addAlarm:alarm];
EKEventEditViewController *eventViewController = [[EKEventEditViewController alloc] init];
eventViewController.event = event;
eventViewController.eventStore=eventStore;
eventViewController.editViewDelegate = self;
[self.navigationController presentModalViewController:eventViewController animated:YES];

这工作正常,只是事件警报属性没有设置,如下图所示:

警报未设置

如果我在显示视图控制器之前保存事件,它确实会设置警报。

请注意我使用的是 LLVM 编译器,所以不用担心无法发布东西!

塔罗斯

I'm using the following code to create an event, and display a popup asking the user to save the event:

EKEventStore *eventStore = [[EKEventStore alloc] init];
EKCalendar *calendar = [eventStore defaultCalendarForNewEvents];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.calendar = calendar;
event.title = [NSString stringWithFormat:@"Event: %@", [self.event title]]; 
event.location = self.event.location;
event.notes = [self stringByStrippingHTML: [self.event description]];
event.startDate = [self.event startDate]; 
event.endDate = [self.event endDate];

NSTimeInterval alarmOffset = -1*60*60;//1 hour
EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:alarmOffset];

[event addAlarm:alarm];
EKEventEditViewController *eventViewController = [[EKEventEditViewController alloc] init];
eventViewController.event = event;
eventViewController.eventStore=eventStore;
eventViewController.editViewDelegate = self;
[self.navigationController presentModalViewController:eventViewController animated:YES];

This works fine except the event alarm property doesn't get set as you can see form the image below:

Alarm Not Set

If I save the event before showing the view controller it does get the alarm set.

Please note I'm using the LLVM compiler, so don't worry about not releasing stuff!

Ta

Ross

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

多孤肩上扛 2024-12-17 01:58:24

终于知道怎么做了。

您的控制器必须实现 UINavigationControllerDelegate 协议,并将 EKEventEditViewController 委托设置为 self。然后只需实现

navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewControllerAnimated:(BOOL)animated

方法并在那里添加警报。

这是我的实现。

- (void)navigationController:(UINavigationController *)navigationController 
  willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {

    if ([navigationController isKindOfClass:[EKEventEditViewController class]]) {
      EKEventEditViewController *ek = (EKEventEditViewController*)navigationController;
      EKEvent *event = ek.event;
      // set alarm to 15 mins prior of the event if it starts later than 15 mins out
      if ([event.startDate compare:[[NSDate date] dateByAddingTimeInterval:60*15]] != NSOrderedAscending) { 
        EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:60*15*-1];
        event.alarms = [NSArray arrayWithObject:alarm];
      }    
    }
}

Finally found out how to do it.

Your controller would have to implement the protocol UINavigationControllerDelegate, and set the EKEventEditViewController delegate to self. Then just implement the

navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated

method and add the alarm there.

Here is my implementation.

- (void)navigationController:(UINavigationController *)navigationController 
  willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {

    if ([navigationController isKindOfClass:[EKEventEditViewController class]]) {
      EKEventEditViewController *ek = (EKEventEditViewController*)navigationController;
      EKEvent *event = ek.event;
      // set alarm to 15 mins prior of the event if it starts later than 15 mins out
      if ([event.startDate compare:[[NSDate date] dateByAddingTimeInterval:60*15]] != NSOrderedAscending) { 
        EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:60*15*-1];
        event.alarms = [NSArray arrayWithObject:alarm];
      }    
    }
}
彼岸花ソ最美的依靠 2024-12-17 01:58:24
NSTimeInterval alarmOffset = -1*60*60;//1 hour
EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:alarmOffset];

[event addAlarm:alarm];

在呈现控制器之后编写上面的代码,即在该行之后

[self.navigationController presentModalViewController:eventViewController animated:YES];
NSTimeInterval alarmOffset = -1*60*60;//1 hour
EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:alarmOffset];

[event addAlarm:alarm];

Write the above code after presenting the controller i.e after the line

[self.navigationController presentModalViewController:eventViewController animated:YES];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文