iOS开发之EventKitUI框架的应用
前面博客,有介绍EventKit这个框架的使用,使用EventKit可以与系统的日历和提醒应用进行交互,读写用户的日程事件。EventKitUI,顾名思义,其实基于EventKit框架,提供了一套系统的事件管理界面。EventKit的基础内容介绍如下:
https://my.oschina.net/u/2340880/blog/3066175
一、EKCalendarChooser日历选择页面
EKCalendarChooser提供了选择日历,即选择EKCalendar对象的视图控制器,示例如下:
1 2 3 4 5
| EKCalendarChooser *chooser = [[EKCalendarChooser alloc] initWithSelectionStyle:EKCalendarChooserSelectionStyleSingle displayStyle:EKCalendarChooserDisplayAllCalendars eventStore:self.eventStore]; chooser.showsDoneButton = YES; chooser.showsCancelButton = YES; chooser.delegate = self; [self.navigationController pushViewController:chooser animated:YES];
|
需要注意,在实例化EKCalendarChooser的时候,需要关联一个EKEventStore对象,用来进行数据操作。
EKCalendarChooser中属性方法如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
- (id)initWithSelectionStyle:(EKCalendarChooserSelectionStyle)selectionStyle displayStyle:(EKCalendarChooserDisplayStyle)displayStyle eventStore:(EKEventStore *)eventStore;
- (id)initWithSelectionStyle:(EKCalendarChooserSelectionStyle)style displayStyle:(EKCalendarChooserDisplayStyle)displayStyle entityType:(EKEntityType)entityType eventStore:(EKEventStore *)eventStore;
@property(nonatomic, copy) NSSet<EKCalendar *> *selectedCalendars;
@property(nonatomic, readonly) EKCalendarChooserSelectionStyle selectionStyle;
@property(nonatomic, weak, nullable) id<EKCalendarChooserDelegate> delegate;
@property(nonatomic) BOOL showsDoneButton;
@property(nonatomic) BOOL showsCancelButton;
|
EKCalendarChooserDelegate代理中定义的方法如下:
1 2 3 4 5 6 7 8 9
| @protocol EKCalendarChooserDelegate <NSObject> @optional
- (void)calendarChooserSelectionDidChange:(EKCalendarChooser *)calendarChooser;
- (void)calendarChooserDidFinish:(EKCalendarChooser *)calendarChooser;
- (void)calendarChooserDidCancel:(EKCalendarChooser *)calendarChooser; @end
|
二、EKEventViewController事件详情页面
EKEventViewController提供了展示某个事件详情的试图控制器,示例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| - (void)queryEvent { for (EKCalendar *cal in [self.eventStore calendarsForEntityType:EKEntityTypeEvent]) { if ([cal.title isEqualToString:@"珲少的事项日历"]) { NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *oneMonthFromNowComponents = [[NSDateComponents alloc] init]; oneMonthFromNowComponents.month = 1; NSDate *oneMonthFromNow = [calendar dateByAddingComponents:oneMonthFromNowComponents toDate:[NSDate date] options:0];
NSPredicate*predicate = [self.eventStore predicateForEventsWithStartDate:[NSDate date] endDate:oneMonthFromNow calendars:@[cal]]; NSArray *eventArray = [self.eventStore eventsMatchingPredicate:predicate]; EKEventViewController *controller = [[EKEventViewController alloc] init]; controller.event = eventArray.firstObject; [self presentViewController:controller animated:YES completion:nil]; } } }
|
EKEventViewController也支持进行事件的编辑,其中属性方法如下:
1 2 3 4 5 6 7 8 9 10
| @interface EKEventViewController : UIViewController
@property(nonatomic, weak, nullable) id<EKEventViewDelegate> delegate;
@property(nonatomic, retain, null_unspecified) EKEvent *event;
@property(nonatomic) BOOL allowsEditing;
@property(nonatomic) BOOL allowsCalendarPreview; @end
|
EKEventViewDelegate中只定义了一个方法,如下:
1 2 3 4 5 6 7 8 9 10 11 12
| @protocol EKEventViewDelegate <NSObject> @required
- (void)eventViewController:(EKEventViewController *)controller didCompleteWithAction:(EKEventViewAction)action __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_2); @end
|
三、EKEventEditViewController事件编辑控制器
EKEventEditViewController提供了事件编辑的视图控制器,对于可编辑的EKEventViewController视图控制器,当用户点击的编辑按钮后,也会调用EKEventEditViewController视图控制器进行编辑,示例如下:
1 2 3
| EKEventEditViewController *controller = [[EKEventEditViewController alloc] init]; controller.event = eventArray.firstObject; [self presentViewController:controller animated:YES completion:nil];
|
其中属性方法如下:
1 2 3 4 5 6 7 8 9 10
| @interface EKEventEditViewController : UINavigationController
@property(nonatomic, weak, nullable) id<EKEventEditViewDelegate> editViewDelegate;
@property(nonatomic, retain, null_unspecified) EKEventStore *eventStore;
@property(nonatomic, retain, nullable) EKEvent *event;
- (void)cancelEditing; @end
|
EKEventEditViewDelegate解析如下:
1 2 3 4 5 6 7 8
| @protocol EKEventEditViewDelegate <NSObject> @required
- (void)eventEditViewController:(EKEventEditViewController *)controller didCompleteWithAction:(EKEventEditViewAction)action; @optional
- (EKCalendar *)eventEditViewControllerDefaultCalendarForNewEvents:(EKEventEditViewController *)controller; @end
|