使用 EKEventEditViewController 创建带有闹钟的日历事件
我使用以下代码创建一个事件,并显示一个弹出窗口,要求用户保存该事件:
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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
终于知道怎么做了。
您的控制器必须实现 UINavigationControllerDelegate 协议,并将 EKEventEditViewController 委托设置为 self。然后只需实现
navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewControllerAnimated:(BOOL)animated
方法并在那里添加警报。
这是我的实现。
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.
在呈现控制器之后编写上面的代码,即在该行之后
Write the above code after presenting the controller i.e after the line